2013/02/11

Sieve of Eratosthene

This is the "sieve of Eratosthenes" so-called.
It can find the prime numbers up to 10000.

int fscroll=YES:// YES=scroll(V2.0 only) / NO=page(all version)
if isLocalizeJapan() then {
    print "エラトステネスのふるい(〜10000)"
} else {
    print "Sieve of Eratosthenes(to 10000)"
}
print "start:";time$
//
int i,p
int max=10000
dim nt(10000):/* At definition , initial value is 0(=NO)
for i=2 to max/2
   nt(i*2)=YES
next
p=3
while p*p<=max
   for i=p to max/p
      nt(i*p)=YES
      i=i+1:/* step2
   next
   p=p+1
   while nt(p):p=p+1:endwhile
endwhile
int cnt=0
for i=2 to max
    if nt(i)=0 then {
        print i,
        if not fscroll then {
          inc(cnt)
          if cnt>=30*8 then {
            // Because X-BASIC for iOS can not scroll text screen, it continues to display with clearing text screen every certain number.
            hitKey()
            cls()
            cnt=0
          }
        }
    }
next
print
print "end:";time$
end
//---------
func hitKey()
    setFunctionKey(0,localizedString("ここを押してください","Hit This Button"),'!')
    displayFunctionKey(YES,0,0)
    while inkey()=0
    endwhile
    cls()
    displayFunctionKey(NO,0,0)
endfunc
//---------
func str localizedString(js;str,es;str)
    if isLocalizeJapan() then return(js)
    return (es)
endfunc
//---------


Zip archive file : XBetc.zip

No comments:

Post a Comment