Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Primes calculation (Ozgur Akgun)
2. Re: Primes calculation (Sriram Durbha)
3. Re: Primes calculation (I. J. Kennedy)
4. Re: Primes calculation (Ozgur Akgun)
5. Re: Primes calculation (Daniel Fischer)
6. Re: Primes calculation (Ozgur Akgun)
----------------------------------------------------------------------
Message: 1
Date: Sat, 13 Mar 2010 03:49:24 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Primes calculation
To: legajid <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
If we try to implement your idea literally we would need one more parameter
to the function: the list of existing primes.
I'll call this function primesWRT, since it finds all primes, with respect
to the second list provided.
primesWRT [] _ = [] -- base case
primesWRT (x:xs) existing
-- if all x is not divided by all of the existing primes,
-- x itself is a prime (wrt 'existing')
-- add it to the primes list, and the existing primes list for the next
function call
| all (\ y -> mod x y /= 0 ) existing = x : primesWRT xs (x:existing)
-- if x is not prime, check the rest.
| otherwise = primesWRT xs existing
primes xs = primesWRT xs []
Please keep in mind that this function assumes its parameter to be in form
[2..n]. But this is an assumption coming from your description.
primes [2..20] = [2,3,5,7,11,13,17,19]
primes [2..30] = [2,3,5,7,11,13,17,19,23,29]
primes [10..30] = [10,11,12,13,14,15,16,17,18,19,21,23,25,27,29] -- not bad
actually, works as advertised.
Best,
On 12 March 2010 21:14, legajid <[email protected]> wrote:
> 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.
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100312/8d4dd033/attachment-0001.html
------------------------------
Message: 2
Date: Fri, 12 Mar 2010 22:32:02 -0800
From: Sriram Durbha <[email protected]>
Subject: Re: [Haskell-beginners] Primes calculation
To: Ozgur Akgun <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
>
> primes [10..30] = [10,11,12,13,14,15,16,17,18,19,21,23,25,27,29] -- not bad
> actually, works as advertised.
>
10 is not prime.
On Fri, Mar 12, 2010 at 7:49 PM, Ozgur Akgun <[email protected]> wrote:
> If we try to implement your idea literally we would need one more parameter
> to the function: the list of existing primes.
> I'll call this function primesWRT, since it finds all primes, with respect
> to the second list provided.
>
> primesWRT [] _ = [] -- base case
> primesWRT (x:xs) existing
> -- if all x is not divided by all of the existing primes,
> -- x itself is a prime (wrt 'existing')
> -- add it to the primes list, and the existing primes list for the next
> function call
> | all (\ y -> mod x y /= 0 ) existing = x : primesWRT xs (x:existing)
> -- if x is not prime, check the rest.
> | otherwise = primesWRT xs existing
>
> primes xs = primesWRT xs []
>
>
> Please keep in mind that this function assumes its parameter to be in form
> [2..n]. But this is an assumption coming from your description.
>
> primes [2..20] = [2,3,5,7,11,13,17,19]
> primes [2..30] = [2,3,5,7,11,13,17,19,23,29]
> primes [10..30] = [10,11,12,13,14,15,16,17,18,19,21,23,25,27,29] -- not bad
> actually, works as advertised.
>
> Best,
>
>
> On 12 March 2010 21:14, legajid <[email protected]> wrote:
>
>> 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.
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
>
> --
> Ozgur Akgun
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100313/5ff66c9e/attachment-0001.html
------------------------------
Message: 3
Date: Sat, 13 Mar 2010 00:39:26 -0600
From: "I. J. Kennedy" <[email protected]>
Subject: Re: [Haskell-beginners] Primes calculation
To: Sriram Durbha <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
> primes [10..30] = [10,11,12,13,14,15,16,17,18,19,21,23,25,27,29] -- not bad
> actually, works as advertised.
FAIL
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100313/bda15149/attachment-0001.html
------------------------------
Message: 4
Date: Sat, 13 Mar 2010 09:55:59 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Primes calculation
To: "I. J. Kennedy" <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Yes, the function fails for [10..30]. But do you ever read the explanations?
On 13 March 2010 06:39, I. J. Kennedy <[email protected]> wrote:
>
> primes [10..30] = [10,11,12,13,14,15,16,17,18,19,21,23,25,27,29] -- not bad
>> actually, works as advertised.
>
>
> FAIL
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100313/c1f77102/attachment-0001.html
------------------------------
Message: 5
Date: Sat, 13 Mar 2010 13:47:38 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Primes calculation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Samstag 13 März 2010 10:55:59 schrieb Ozgur Akgun:
> Yes, the function fails for [10..30]. But do you ever read the
> explanations?
>
You probably should have chosen another signature for primes,
primes n = primesWRT [2 .. n] []
, to avoid the misunderstandings.
------------------------------
Message: 6
Date: Sat, 13 Mar 2010 13:02:45 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Primes calculation
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Yes you're right.
I tried to implement sth to answer the original question. Not something I
came up with.
On 13 March 2010 12:47, Daniel Fischer <[email protected]> wrote:
> Am Samstag 13 März 2010 10:55:59 schrieb Ozgur Akgun:
> > Yes, the function fails for [10..30]. But do you ever read the
> > explanations?
> >
>
> You probably should have chosen another signature for primes,
>
> primes n = primesWRT [2 .. n] []
>
> , to avoid the misunderstandings.
>
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100313/cbcd14c2/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 13
*****************************************