strange behaviour : computing lowest divisor

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Try with Integer rather than Int. Might be an overflow issue...
On Wed, Dec 29, 2010 at 5:32 PM, Abhijit Ray
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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thanks, that seems to have fixed it.
On Wed, Dec 29, 2010 at 5:46 PM, Lyndon Maydwell
Try with Integer rather than Int. Might be an overflow issue...
HI, I have written(copied) a small piece of code which finds the lowest
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
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
On Wed, Dec 29, 2010 at 5:32 PM, Abhijit Ray
wrote: divisor 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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wednesday 29 December 2010 10:59:53, Abhijit Ray wrote:
Thanks, that seems to have fixed it.
On Wed, Dec 29, 2010 at 5:46 PM, Lyndon Maydwell
wrote: Try with Integer rather than Int. Might be an overflow issue...
*Main> ld 278970415063349480483707695
Yes, that number is between 2^87 and 2^88, as an Int, it's typically one of 206297903 = 7*37*796517 (32-bit Ints) or -9158009321667437777 = -7*13*15015953*670203649 (64-bit Ints).
participants (3)
-
Abhijit Ray
-
Daniel Fischer
-
Lyndon Maydwell