My Primes
Hello all:
Since I'm the one who originally corresponded with Carl (regarding
primes), I guess I should put in a small effort. This is pretty much the
same one that Carl posted with the exception of the "top" variable being
computed using the sqrt() function, below.
I'm normally a "list -- lurker", and have done very little Euphoria
programming. I originally tried this program to see how it compared with
a compiled Fortran 90 program that I had written. Results were basically
identical -- which greatly impressed me (for an intrepreted language).
I've run this program with an upper limit of over 3,000,000 (to see what
happens). At that point, the memory is using the disk swap file (slowing
things down tremendously). However, Euphoria handled this better (faster)
than my Fortran progam did. I'm guessing this might be a function of the
Causeway DOS extender that Euphoria uses.
Also, I always get slightly faster times when I run the program a 2nd or
3rd time. I assume this is because the program is in cache memory at that
point.
I'm enjoying following this particular news thread. -- Bob Hancock
! -- ----------------------
include get.e
include machine.e
-- with profile_time
-- without type_check
integer n, s, last, top
atom t1, t2
sequence A, C, m
puts(1, "\n List All PRIMES, up to What Number ? ... \n")
m = get(0)
if m[1] != GET_SUCCESS then
puts(1, "\n Couldn't Read your number ... \n ")
end if
t1 = time()
n = m[2]
top = floor(sqrt(n)) + 1
A = repeat(0, n)
C = repeat(0, n)
for j = 1 to n do
A[j] =j
end for
for j=2 to top do
if A[j] != 0 then -- don't check multiples of zero
for k = 2*j to n by j do
A[k] = 0
end for
end if
end for
puts(1, "\n\n")
s = 1 -- counting the primes
for j = 1 to n do
if A[j] != 0 then
C[s] = A[j]
s = s + 1
end if
end for
last = s - 1 -- the last prime
puts(1, " \n\n ")
puts(1, "\n The Last 5 Primes are ... \n")
printf(1, "%7d \t %7d \t %7d \t %7d \t %7d \n ", C[last - 5 .. last])
printf(1, "\n\n There were %d PRIMES ! ... below the number %d \n ",
{last - 1, n})
t2 = time() - t1
printf(1, " \n Total time = %6.2f", t2)
! -- -----------------------
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Bob Hancock ... ======>bob at speed.net<========
Irvine, CA
|
Not Categorized, Please Help
|
|