Re: little isprime challenge
- From: "James Parsly" <japarsly@xxxxxxxxxxxxxx>
- Date: Wed, 24 Aug 2005 11:47:43 -0400
Would it be faster to compute the square root of p, and use it as the loop
limit?
This would eliminate the need for the 'i*i' test.
If you want to keep the I*I test, there's a way to turn the multiplication
into
addition. Initial a new variable ISQUARE to 9 before the beginning of the
loop
and then update at the end with ISQUARE = ISQUARE + I + I + 1
or ISQUARE = ISQUARE + 2*I+1 (which is better depends on how
clever your compiler is, since multiplying by 2 can often be optimized into
a bit shift operation). Whether this will actually be faster is hard to
say, try it
and see.
"e p chandler" <epc8@xxxxxxxx> wrote in message
news:1124848022.896820.218980@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Bart Vandewoestyne wrote:
>> I know there has been a kind of `string-challenge' in this group
>> which I have not been following... but it's been taking a while
>> and from what I've read I have the feeling that some people got
>> bored of it and now are definitely in need for something else...
>> so I have another little `isprime-challlenge' :-)
>>
>> It's just 100% pure for the fun of it, and maybe it will also
>> teach some more novices and `less experienced' young fortran
>> programmers as me good optimization skills. It might also be usefull for
>> some people in need for some prime routines.
>>
>> The `assignment' would be (I will try to formulate it as formal
>> as possible in order to avoid confusion...):
>>
>> "Write a standard Fortran 95 function isprime(p) that also adheres to
>> the F-language and that checks if the number p is a prime. The routine
>> should be as fast as possible (to be measured with the CPU_TIME
>> intrinsic.
>> 2 is considered to be a prime. The speed of your routine will be
>> measured
>> by checking the numbers 1,n where n is a very big number, whatever
>> `very big' may be ;-)"
>>
>> I've put up a skeleton file that is available online at
>>
>> http://www.cs.kuleuven.ac.be/~bartv/stuff/time_isprime.f95
>>
>> This is the file where you should add your version to.
>>
>> My first result on an Intel(R) Pentium(R) 4 CPU 2.40GHz running
>> a stable Debian GNU/Linux and compiled with the latest
>> F-compiler:
>>
>> bartv@vonneumann:~/fortran$ make time_isprime
>> F -o time_isprime -O4 time_isprime.f95
>> bartv@vonneumann:~/fortran$ ./time_isprime
>> Enter the number of points you want to time for: 10000000
>> Bart's method found 664579 primes in 27.120001 seconds.
>>
>> Good luck!
>> Bart
>> :-)
>>
>> --
>> "Share what you know. Learn what you don't."
>
> I'm going to go for the "fairly easy" and post the result of a simple
> modification to your function. Instead of testing the candidate against
> all numbers from 2 and up, why not test for 2, then test for
> divisibility by 2, then test against odd numbers from 3 and up?
>
> The core of my function is:
>
> res = .false.
>
> if (p < 2) then
> res = .false.
> return
> else if (p == 2) then
> res = .true.
> return
> else if (modulo(p,2) == 0) then
> res = .false.
> return
> else
> do i=3,p,2
> if (i*i > p) then
> res = .true.
> return
> else if (modulo(p,i) == 0) then
> res = .false.
> return
> end if
> end do
> end if
>
> Here's a run on a 2.8 GHz P4-HT under WinXP(SP2):
>
> C:\temp>F -O4 -o test.exe test.f95
>
> C:\temp>test
> Enter the number of points you want to time for: 10000000
> Bart's method found 664579 primes in 23.156250 seconds.
> Elliot's method found 664579 primes in 11.656250
> seconds.
>
> This took only a few minutes to write and test but it cut the run time
> nearly in half.
>
> If I was doing this program in ye olden days, I would have put in a
> COMMON block which contains an array holding the primes found so far.
> Then I would have divided by the list of primes already found. Of
> course F does not allow COMMON blocks. :-).
>
.
- Follow-Ups:
- Re: little isprime challenge
- From: Rich Townsend
- Re: little isprime challenge
- References:
- little isprime challenge
- From: Bart Vandewoestyne
- Re: little isprime challenge
- From: e p chandler
- little isprime challenge
- Prev by Date: Re: maximum array size for g77 compiler
- Next by Date: Re: ANN: f95totex: A Fortran-to-LaTeX prettyprinter (of sorts)
- Previous by thread: Re: little isprime challenge
- Next by thread: Re: little isprime challenge
- Index(es):
Relevant Pages
|