Submitted by MGA on

I want to offer fresh code to library that might be of interest to others but not sure how to go about doing that lets see if I can copy it straight into here?

'First Factors MGA.BAS 10/19/14

'I was challenged/inspired by the featured Primes.BAS code in SmallBASIC forum yesterday

'First Factors works from a different angle producing an array that is actually useful

'for finding more than just prime numbers. Use it to factor any number up to...

'topN, I left it at 1223 because that is the 200th prime found with Primes.BAS

'on my machine and system I get .002 or .004 secs for 200 primes but .02 something with Primes.BAS

'of course I've gone up to 100,000 for TopN with .2+ secs results

'The algorithm is based on Sieve of Eratostheses, old as Greek Geometry with some prime number study added

'Ha! looking up how to spell Sieve of Eratostheses I learn of Euler's Sieve and his Theta Numbers (Wiki)

'n+1... there is always more that one can do, if one can do something!

'THANK YOU SmallBASIC crew and the Open Code Spirit - MGA

option base 1

topN=1223

testlimitN=sqr(topN)

dim ffA(topN+6)

'In first FOR loop:

'you could go nuts performance wise and build patterns with 30 or 210 which are prime factorials

'here is just 6=2*3 which is an improvement on just 2 odds and evens.

'Prime factorials: 30=2*3*5 and 210=2*3*5*7... these create ever expanding and overlapping patterns

tock=ticks

for i = 0 to topN step 6

ffA(i+1)=1:ffA(i+2)=2:ffA(i+3)=3:ffA(i+4)=2:ffA(i+5)=1:ffA(i+6)=2

next

ffa(2)=1:ffa(3)=1 'fix first two primes

for pcand=5 to testlimitN step 2

if ffA(pcand)=1 then

for i=pcand+pcand to topN step pcand

if ffA(i)=1 then ffA(i)=pcand : i=i+pcand 'booster shot .02 secs on 100,000

next

fi

next

tic=ticks

?

print topN; " First Factors Array Complete in ";(tic-tock)/tickspersec;" secs."

?

pcount=0

for i =2 to topN

if ffA(i)=1 then pcount =pcount +1

next

tic =ticks

print "In first ";topN;" numbers there are ";pcount;" Primes."

?

print " In case a prime is not a prime until it is counted as one:"

? "It took "; (tic-tock)/tickspersec; " secs to load First Factor Array and count it's Primes."

?

Print" press any "

?

pause

'comment next goto in or out depending whether you want the listing, you might not want to see

'all 100,000 and above

'goto jumpover

for i=2 to topN

if ffa(i)=1 then

print i,

count=count +1

if frac(count/10)=0 then print

if frac(count/200)=0 then ?:?" press any":pause :Cls

fi

next

label jumpover

xn=6

cls

label FactorNumber

?

? "Enter a number up to "+TopN+" to factor ( <2 to quit ) ";

input xn

if xn >= 2 and xn <=topN then

? xn; " factors are: ",

while ffA(xn)<>1

print ffa(xn),

xn=xn/ffa(xn)

wend

print xn

?

else

'I have learned with SmallBASIC we need to say when we are done

'because in SmallBASIC we can go back and look inside our code while

'the program is running, its nice feature but can mess you up if you

'forget the program is still running

?:? "That's all Folks!"

END

fi

goto FactorNumber

well, there it is, hope someone likes it - MGA Thanks Again!

Written in Windows 7, with version 0.11.5 from Source Forge

By the way, while I am here, I have to say I had a blast with TinyBasic making it tinier by making all the commands one letter as well and adding X^Y power to it the first week with SmallBASIC. Too bad it's not too practical, to have to know ahead of time where to goto every time you write a GOSUB or M as I recoded it (or was it B). I love the pure simplicity.

I guess I am at my limit (on this screen), oh I can go on... and ELIZA, oh ELIZA! what fun I had playing with that b... basic code. "She" is now out in a "script" data file and I can emulate her, if I want, but I'd rather dialog other characters of my own making on what is now called a Player (of Script files, you know like a record player or CD... why not a script, really that is what BASIC is). I am working on a script for a Zen Master, actually all you are doing is dialoging with your imagination.

I am noticing that this Forum seems to have seen brighter days, the code library is ancient. Is everyone going over to the other, other Small Basic? I confess I had a look see last night and downloaded my forth version of BASIC. It's Output screen, one of them, is certainly less cranky more well behaved, but who wants to address it with such a string of typing when you have an idea (such as I did above) and want to test it fast? and "?" does the job so much faster! They do have manuals, they do walk you through "The Change". I confess the output screen here is not for these old GW BASIC eyes the print too fine. It leaves acres and acres of space because I suppose SmallBASIC is geared to handheld newfangled contraptions. And the "apps" I try out are printing all over themselves on my screen in a tiny little corner upper left. I did say I love simplicity, I did say part of me longs to be a Zen Master but when text writes all over itself we call that getting in one's own way.

I digress, I rave!, I do carry on...

Thank you again SmallBASIC crew and Open Source Spirit!

We've reawakened a spirit of...

MGA version 10/20/14 12.16p

- Log in to post comments