[Haskell-beginners] Primes calculation

legajid legajid at free.fr
Fri Mar 12 16:14:00 EST 2010


Hi,
i'm trying to write a function to find all primes from 2 to N.


My idea is :
    take the first number (2)
    try to find whether it's a multiple of one of all existing primes 
([] at first)
    add 2 to the list of primes

    take the following number (3)
    find if multiple of existing primes ([2])
    add 3 to the list of primes

    take the following number (4)
    find if multiple of existing primes ([2, 3])
    do not add 4 to the list of primes

    ...

    take the following number (8)
    find if multiple of existing primes ([2, 3, 5, 7])
    do not add 8 to the list of primes

    take the following number (9)
    find if multiple of existing primes ([2, 3, 5, 7])
    do not add 9 to the list of primes (multiple of 3)

    and so on...

So, i would like a function like :

    f (x : xs)  = g  x  :  f  xs

g would  return  x  if  x is prime, []  otherwise

But g would use the preceding value of  f  (list of primes before  the  
calculation for x) that is a result of g itself.
f needs g that needs f : what's wrong with my mind ?
Perhaps i am still under control of imperative programming ?

Thanks for your help,

Didier.


 



More information about the Beginners mailing list