[Haskell-beginners] strange behaviour : computing lowest divisor

Abhijit Ray avizit at gmail.com
Wed Dec 29 10:32:50 CET 2010


HI,
I have written(copied) a small piece of code which finds the lowest divisor
of am integer (greater than 1)

the code is here


ld::Int->Int
ld n = ld' 2 n
       where
  ld' i n | rem n i == 0 = i
                 | i^2 > n = n
                 | otherwise = ld' (i+1) n


now it seems to work fine for small numbers but for a big number we get

*Main> ld 278970415063349480483707695
7

the number is obviously divisible by 5 , so where is the anomaly ?

Thanks,
Abhijit
On Wed, Dec 29, 2010 at 2:52 PM, Alex Rozenshteyn <rpglover64 at gmail.com>wrote:

> I'm trying to understand the technique referred to as "tying the knot", but
> documentation on the internet seems to be much sparser and more obtuse than
> I like.
>
> So I'm asking here.
>
> As far as I understand, "tying the knot" refers to a way of using laziness
> to implement something like references in a purely functional way.
>
> I'm trying to write a toy simulation:
> I have a population :: [Person]
> I want to collect a random subset of possible pairs of distinct people.
> So I go to each person in the population and select a subset of the people
> after him/her in the list; these are pairs in which s/he is the first
> element.
>
> I want to then be able to ask for all pairs in which a person is the first
> or the second element.  I could give each person a unique id, but it seems
> like tying the knot is a valid way to implement this.
>
> Please correct me if I am misunderstanding.
>
> Thank you.
>
> --
>            Alex R
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20101229/18fb7c4a/attachment.htm>


More information about the Beginners mailing list