Dark Bit Factory & Gravity

PROGRAMMING => Other languages => Yabasic => Topic started by: bikemadness on January 08, 2018

Title: Prime Numbers
Post by: bikemadness on January 08, 2018
Couldn't find any samples of working out prime numbers
So here's mine.

Code: [Select]
open window 640,512
setrgb 1,200,200,200
space=12
spaces=int(490/space)
lines=0
gap=0
z=3500
repeat
lines=0
gap=0
number=0
for y=1 to z
num=0
for x=1 to y
if frac(y/x)=0 num=num+1
next x
if num<3 then
lines=lines+1
number=number+1
text 10+gap,lines*space,str$(y)
endif
if lines>spaces then
   gap=gap+50
   lines=0
   endif
next y
text gap,(lines+1)*space,"("+str$(number)+")"
until (1=0)
Title: Re: Prime Numbers
Post by: combatking0 on January 08, 2018
Interesting. If I were coming up with my own algorithm I'd have to seed it with the first 2 primes and set it going from there. Though I don't imagine mine would be very elegant.

I'll definitely have to give this one a try.
Title: Re: Prime Numbers
Post by: bikemadness on January 09, 2018
Thanks. I've now added the numerical count
and a start off for larger numbers so the whole
screen doesn't get filled.

Code: [Select]
open window 640,512
space=12
spaces=int(500/space)
lines=0
gap=0
z=int(spaces*87)
repeat
lines=0
gap=0
number=0
for y=1 to 10000
num=0
for x=1 to y
if frac(y/x)=0 num=num+1
next x
if num<3 number=number+1
if num<3 and y>9000 then
'won't start texting until after 9000
'delay until then, since still has to count
lines=lines+1
setrgb 1,0,256,0
text gap,lines*space,str$(number)
setrgb 1,256,256,256
text 50+gap,lines*space,str$(y)
endif
if lines>spaces then
   gap=gap+90
   lines=0
   endif
next y
until (1=0)
Title: Re: Prime Numbers
Post by: bikemadness on January 10, 2018
After a little thought, I sped it up a little, simplified it
and added clear screen carry on. It has room for 6 digits.

Code: [Select]
open window 640,512
setrgb 1,256,256,256
for y=1 to 1000000
num=0
for x=1 to y
if frac(y/x)=0 num=num+1
if num>4 goto skip
next x
if num<3 then
lines=lines+1
number=number+1
nos=nos+1
text 5+gap,lines*11,str$(y)
endif
label skip
if lines>45 then
   gap=gap+70
   lines=0
   endif
if nos=414 then
inkey$
clear window
nos=0
gap=0
lines=0
endif
next y
Title: Re: Prime Numbers
Post by: bikemadness on January 12, 2018
And a little faster.
If it only showed text for the last few,
it would take half the time. Use rem notes.

Code: [Select]
open window 640,512
setrgb 1,256,256,256
for y=1 to 100000
num=0
for x=1 to sqrt(y)
if frac(y/x)=0 num=num+1
if num>3 goto skip
next x
if num<2 then
lines=lines+1
number=number+1
nos=nos+1
text 5+gap,lines*11,str$(y)  rem place with this: if y>99950 text 5+gap,lines*11,str$(y)
endif
label skip
if lines>45 then
   gap=gap+70
   lines=0
   endif
if nos=414 then
inkey$ rem take out this
clear window
nos=0
gap=0
lines=0
endif
next y
text gap,(lines+1)*11,"("+str$(number)+")"
Title: Re: Prime Numbers
Post by: combatking0 on January 12, 2018
On the topic of primes, I hear some asymmetric encryption algorithms are based on the product of two large prime numbers.

I wonder if it's possible to make an asymmetric encryption algorithm using the product of three or more prime numbers? That could (and maybe should) be a topic of its own, but it's interesting to consider at the very least.