Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 919589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:34:13+00:00 2026-05-15T18:34:13+00:00

I am solving some problems of Project Euler in Haskell. I wrote a program

  • 0

I am solving some problems of Project Euler in Haskell. I wrote a program for a riddle in it and it did not work as I expected.

When I looked in the task manager when running the program I saw that it was using > 1 gigabyte of RAM on ghc. A friend of me wrote a program with the same meaning in Java and succeeded in 7 seconds.

import Data.List

opl = find vw $ map (\x-> fromDigits (x++[0,0,9]) ) 
        $ sequence [[1],re,[2],re,[3],re,[4],re,[5],re,[6],re,[7],re,[8],re]

vw x = hh^2 == x
    where hh = (round.sqrt.fromIntegral) x

re = [0..9]

fromDigits x = foldl1 (\n m->10*n+m) x

I know this program would output the number I want given enough RAM and time, but there has to be a better-performing way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:34:14+00:00Added an answer on May 15, 2026 at 6:34 pm

    The main problem here is that sequence has a space leak. It is defined like this:

    sequence [] = [[]]
    sequence (xs:xss) = [ y:ys | y <- xs, ys <- sequence xss ]
    

    so the problem is that the list produced by the recursive call sequence xss is re-used for each of the elements of xs, so it can’t be discarded until the end. A version without the space leak is

    myseq :: [[a]] -> [[a]]
    myseq xs = go (reverse xs) []
     where
      go [] acc = [acc]
      go (xs:xss) acc = concat [ go xss (x:acc) | x <- xs ]
    

    PS. the answer seems to be Just 1229314359627783009

    Edit version avoiding the concat:

    seqlists :: [[a]] -> [[a]]
    seqlists xss = go (reverse xss) [] []
     where
       go []       acc rest = acc : rest
       go (xs:xss) acc rest = foldr (\y r -> go xss (y:acc) r) rest xs
    

    note that both of these versions generate the results in a different order from the standard sequence, so while they work for this problem we can’t use one as a specialised version of sequence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While solving some Project Euler Problems to learn Haskell (so currently I'm a completly
When programming in Haskell (and especially when solving Project Euler problems, where suboptimal solutions
I got nearly no knowledge of haskell and tried to solve some Project Euler
I'm playing with Haskell and Project Euler's 23rd problem. After solving it with lists
I'm solving simple math task, and I have faced with some problems. I have
I'm learning Haskell and solving some programming problems on spoj.pl. The idea of the
I recently started solving Project Euler problems in Scala, however when I got to
I've started working on some Project Euler problems, and have solved number 4 with
I'm doing a project that involves solving some NP-hard graph problems. Specifically triangulation of
I'm solving some classic problems in Haskell to develop my functional skills and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.