Hi, I am doing some further learning with euler now in Haskell instead of Julia. But I am getting something strange. [x+y+z | x <- a2, y <- b3, z <- c4, (x+y+z) < 50] a2, b3, c4 are all finite lists derived from the lazy list of the primes package. the output is: [28,47,33,49 this is not a typo it is still calculating hence the missing bracket (for the last couple of hours, when to a christmas fair in between). The only thing is that, this should be the answer: [28,47,33,49] so it should be done but its not. what am I doing wrong? best, _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
How big are those lists? GHC will not be able to work out x+y+z < 50 will never be true again, it will have to exhaustively consider *every* combination. On Sat, 23 Nov 2019, 6:38 pm Alexander Chen, <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
In reply to this post by Alexander Chen
Hello Alexander,
On Sat, Nov 23, 2019 at 07:38:03PM +0100, Alexander Chen wrote: > Hi, > > I am doing some further learning with euler now in Haskell instead of Julia. But I am getting something strange. > > [x+y+z | x <- a2, y <- b3, z <- c4, (x+y+z) < 50] > > a2, b3, c4 are all finite lists derived from the lazy list of the primes package. the output is: > > [28,47,33,49 λ> :m Data.Numbers.Primes λ> let a = take 100 primes λ> length [x+y+x | x <- a, y <- a, z <- a, (x+y+z) < 50] 942 I suspect one in [a2, b3, c4] is infinite (or very very long). Can you paste the whole calculation? -F _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Hi Francesco, length a2 = 7072 length b3 = 7072 length c4 = 82 I suspect its the first two that get it into trouble. Is is 7072*7072*82 = 41014081088 permutations, 100*3 = 1000000. So I am guessing that this is not the fix to the euler problem.... best. November 23, 2019 8:05:32 PM CET Francesco Ariis <[hidden email]> wrote:"> Hello Alexander, _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
On Sun, Nov 24, 2019 at 05:10:17PM +0100, Alexander Chen wrote:
> Hi Francesco, > > length a2 = 7072 > length b3 = 7072 > length c4 = 82 > > I suspect its the first two that get it into trouble. Is is 7072*7072*82 = 41014081088 permutations, 100*3 = 1000000. > > So I am guessing that this is not the fix to the euler problem.... If `x+y+z` need to be <50, and the three are primes, I am puzzled to see such lengths! Which problem is it? _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Hi Francesco, Their is a ^2, ^3, ^4 in the lists, respectively. best, November 24, 2019 7:19:51 PM CET Francesco Ariis <[hidden email]> wrote:"> On Sun, Nov 24, 2019 at 05:10:17PM +0100, Alexander Chen wrote: _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
On Mon, Nov 25, 2019 at 08:55:17AM +0100, Alexander Chen wrote:
> Hi Francesco, > > Their is a ^2, ^3, ^4 in the lists, respectively. > > see https://projecteuler.net/problem=87 This runs reasonably fast even on my old 32bit machine: import qualified Data.Set as S import Data.Numbers.Primes sol72 :: Integer -> Int sol72 l = S.size . S.fromList $ [(x+y+z) | x <- p2, y <- p3, z <- p4, x+y+z < l] where f :: Integer -> [Integer] f n = takeWhile (< l) $ map (^n) primes p2, p3, p4 :: [Integer] p2 = f 2 p3 = f 3 p4 = f 4 λ> sol72 (50 * 10^6) 1097343 Notice how the lengths for lists p2, p3 and p4 are actually 908, 73 and 23! -F _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |