Sign in to follow this  
Followers 0
bonebreaker

Some prime udfs of mine

6 posts in this topic

#1 ·  Posted (edited)

I finaly managed to bring the primesearchs time to 0.9 sec for 10000 on my machine.It would be much slower if i redimed the array every time it finds a new prime, but its more pointless i know.

I made small yet challanging program to demonstrate a use of the udfs:o

If you want to make the game more difficult reduce the time and increase $place 's starting amount.

Oh, and don't forget to post your scores :whistle:

;===============================================================================
;
; Function Name:   _PrimeSearch
; Description::    Find all primes until the specified limit
; Parameter(s):    $searchlimit - search limit
; Return Value(s):  array[0] contains the amount of primes found
;                   array[x] contains the xth prime
; Author(s):       bonebreaker
;
;===============================================================================
;
func _PrimeSearch($searchlimit)
    local $i=5,$primes[int($searchlimit/4)],$primenum,$step=2
    $primes[3]=5
    $primes[2]=3
    $primes[1]=2
    $primes[0]=3
    while $i<$searchlimit 
        $primenum=3
        $i+=$step
        $step=6-$step
        while $i >= ($primes[$primenum]*$primes[$primenum])
            if mod($i,$primes[$primenum])=0 Then continueloop 2
            $primenum+=1
        wend
        $primes[0]+=1
        $primes[$primes[0]]=$i
    wend
    redim $primes[$primes[0]+1]
    return $primes
endfunc ;<=== _PrimeSearch
;===============================================================================
;
; Function Name:   _IsPrime
; Description::    Check if a number is prime or not
; Parameter(s):    $number - Number to check
; Return Value(s): 1 - Number is prime
;                   0 - Number is not prime
; Author(s):       bonebreaker
;
;===============================================================================
;
func _IsPrime($number)
    local $divisor=5,$step=2
    if ($number <> 2) and (mod($number,2)=0) then return 0
    if ($number <> 3) and (mod($number,3)=0) then return 0
    while $number >= ($divisor*$divisor)
        if mod($number,$divisor)=0 Then return 0
        $divisor+=$step
        $step=6-$step
    wend
    return 1
endfunc ;<=== _IsPrime
;===============================================================================
;
; Function Name:   _PrimeDivide
; Description::    Divide a number into primes
; Parameter(s):    $number - The number to divide
; Return Value(s):  array[0] contains the amount of divisors used
;                   array[x] contains the xth divisor
; Author(s):       bonebreaker
;
;===============================================================================
;
func _PrimeDivide($number)
    local $divisor=2,$divisors[1],$step=2
    $divisors[0]=0
    while 1
        if mod($number,$divisor) then
            $divisor +=1
            exitloop
        else
            $divisors[0] +=1
            redim $divisors[$divisors[0]+1]
            $divisors[$divisors[0]]=$divisor
            $number=$number/$divisor
        endif
    wend
    while 1
        if mod($number,$divisor) then
            $divisor +=2
            exitloop
        else
            $divisors[0] +=1
            redim $divisors[$divisors[0]+1]
            $divisors[$divisors[0]]=$divisor
            $number=$number/$divisor
        endif
    wend
    while $number > 1
        if mod($number,$divisor) then
            $divisor +=$step
            $step=6-$step
        else
            $divisors[0] +=1
            $step=6-$step
            redim $divisors[$divisors[0]+1]
            $divisors[$divisors[0]]=$divisor
            $number=$number/$divisor
        endif
    wend
    return $divisors
endfunc ;<=== _PrimeDivide

primeitive.au3

Edited by bonebreaker

My programs:[topic="40019"]Sudoku[/topic], [topic="41258"]Suitcase game & Random jokes[/topic], [topic="41641"]8 Queens[/topic], [topic="41679"]Puzzle Generator[/topic], [topic="42045"]Snake[/topic]My UDFs:[topic="41309"]Prime functions & Prime game[/topic]Other:Fake emailer

Share this post


Link to post
Share on other sites



Added a small game to demonstrate that primes are fun :whistle:


My programs:[topic="40019"]Sudoku[/topic], [topic="41258"]Suitcase game & Random jokes[/topic], [topic="41641"]8 Queens[/topic], [topic="41679"]Puzzle Generator[/topic], [topic="42045"]Snake[/topic]My UDFs:[topic="41309"]Prime functions & Prime game[/topic]Other:Fake emailer

Share this post


Link to post
Share on other sites

Test the game, I can't play it.

I press New Game when it starts, then the number disappears. At that point it works, but I can't see the number.. >.<

Share this post


Link to post
Share on other sites

Test the game, I can't play it.

I press New Game when it starts, then the number disappears. At that point it works, but I can't see the number.. >.<

That can only happen if you haven't created the primes.txt file in the scripts directory with this script in the first post.

I didn't want to upload a huge txt file..

$primes=_PrimeSearch(10000)
$file=fileopen("primes.txt",2)
FileWriteLine($file,$primes[0])
for $i=1 to $primes[0]
    FileWriteLine($file,$primes[$i])
next
fileclose($file)

func _PrimeSearch($searchlimit)
    local $i=5,$primes[int($searchlimit/4)],$primenum,$step=2
    $primes[3]=5
    $primes[2]=3
    $primes[1]=2
    $primes[0]=3
    while $i<$searchlimit
        $primenum=3
        $i+=$step
        $step=6-$step
        while $i >= ($primes[$primenum]*$primes[$primenum])
            if mod($i,$primes[$primenum])=0 Then continueloop 2
            $primenum+=1
        wend
        $primes[0]+=1
        $primes[$primes[0]]=$i
    wend
    redim $primes[$primes[0]+1]
    return $primes
endfunc

My programs:[topic="40019"]Sudoku[/topic], [topic="41258"]Suitcase game & Random jokes[/topic], [topic="41641"]8 Queens[/topic], [topic="41679"]Puzzle Generator[/topic], [topic="42045"]Snake[/topic]My UDFs:[topic="41309"]Prime functions & Prime game[/topic]Other:Fake emailer

Share this post


Link to post
Share on other sites

Post your score, so I can have a clue about how dumb I am:D

Anyway i rediscovered fileexist function so the script is now stand-alone:)


My programs:[topic="40019"]Sudoku[/topic], [topic="41258"]Suitcase game & Random jokes[/topic], [topic="41641"]8 Queens[/topic], [topic="41679"]Puzzle Generator[/topic], [topic="42045"]Snake[/topic]My UDFs:[topic="41309"]Prime functions & Prime game[/topic]Other:Fake emailer

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0