Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime number generator
#11
Hrmm. So it's Sieve. I'd just translated the title from Dutch, so I didn't know the exact English title. Anyway, I'm going home now... Wink
Reply
#12
Ok, here it is:

Code:
'    ==============
'     Prime Module
'By Zack & Neo Deus Ex Machina
'
'This Module is part of the KetOn library
'made by members of HAR-SoftWare
'
'This Module contains:
' 2 Prime Routines
'
'Test Function (1 routine)
DECLARE FUNCTION KT.PRIME.IsPrime% (NumberToCheck AS LONG)

'Sea Function (1 routine)
DECLARE SUB KT.PRIME.Erathetos (ArrayToFill() AS INTEGER, StartNumber AS LONG)
'$DYNAMIC
DEFINT A-Z

'###############
' Test Function
'###############
FUNCTION KT.PRIME.IsPrime (NumberToCheck AS LONG)
    'IsPrime function, By Zack
    FOR i& = 2 TO NumberToCheck \ 2   'Start the loop that will test to see if the number isn't prime
        IF NumberToCheck / i& = NumberToCheck \ i& THEN   'If the number isn't prime
                    KT.PRIME.IsPrime = 0      'Return 0
                        EXIT FUNCTION      'Exit the function
                END IF
        NEXT i&
        KT.PRIME.IsPrime = -1   'If the number is prime, return -1
END FUNCTION

'##############
' Sea Function
'##############
SUB KT.PRIME.Erathetos (ArrayToFill() AS INTEGER, StartNumber AS LONG)
    'Erathetos function, By Neo Deus Ex Machina
        endnumber& = UBOUND(ArrayToFill) + StartNumber    'calculate the last number to check
        begNumber& = StartNumber \ 2
        IF begNumber& < 2 THEN begNumber& = 2
        FOR i& = 0 TO UBOUND(ArrayToFill)    'fill the array with &HFFFF
            ArrayToFill(i&) = -1
        NEXT i&
        loopto& = endnumber& \ 2    'last loop number
        FOR i& = 2 TO loopto&
            begNumber& = StartNumber \ i&
            IF begNumber& < 2 THEN begNumber& = 2
            FOR inloop& = begNumber& TO endnumber& \ i&
                multiplication& = inloop& * i&    'this is no prime
                IF multiplication& >= StartNumber THEN ArrayToFill(multiplication& - StartNumber) = 0    'put in array that this is no prime
            NEXT inloop&
        NEXT i&
END SUB

Hope it helps!
Reply
#13
Btw, how's this?:

Quote:All primes:
up to 1,000,000: 16,6 seconds
up to 10,000,000: 780 seconds

:roll:
Reply
#14
dude, thats only like, 8 digits. what are you scared? Big Grin jk!

actually its a shard less than 1/10th of all numbers within 8 digits...
Reply
#15
I know, but if I run the program during the night, I might reach, or get over the 100,000,000 primes Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)