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 7447451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:36:25+00:00 2026-05-29T12:36:25+00:00

I’ve recently taken to learning me a haskell and have been having a pretty

  • 0

I’ve recently taken to learning me a haskell and have been having a pretty good time. I’ve been working through some Project Euler problems to get a grip of the syntax and have been reviewing the solutions posted here http://www.haskell.org/haskellwiki/Euler_problems/1_to_10 as a learning tool. Though I have found myself unable to wrap my head around the solution posted for problem #3:

-- Find the largest prime factor of 317584931803. 

primes = 2 : filter ((==1) . length . primeFactors) [3,5..]

primeFactors n = factor n primes
  where
    factor n (p:ps) 
        | p*p > n        = [n]
        | n `mod` p == 0 = p : factor (n `div` p) (p:ps)
        | otherwise      = factor n ps

problem_3 = last (primeFactors 317584931803)

I can’t figure out for the life of me how it works. primes and primeFactors seem to be calling each other to build their own lists and trying to follow it pickles my brain. Any one know a good blog post about this solution or want to write an explanation about it here?

  • 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-29T12:36:26+00:00Added an answer on May 29, 2026 at 12:36 pm

    This is indeed puzzling at first sight. But it’s no magic if you don’t think “imperatively”. Haskell definitions are just that: the tell you what something is, not what lower level operations the computer is supposed to execute.

    Hence, the list of primes is the list that contains 2 and all odd natural numbers greater 2 that have only a single prime factor (namely itself).

    The list of prime factors for some integer n, on the other hand, is the list of prime numbers that divide n.

    Make sure you understand those definitions, before reading on.

    As abstract Haskell may be, it is still a programming language, so we need to give some advice from time to time how to compute something. Specifically, in the example above, we do not test all prime numbers to find prime factors of n, because it is enough to test 2..k where k*k <= n.
    This also makes sure that we only use that part of prime which is already computed.

    At the start, primes looks like this:

    2 : filter ((==1) . length . primeFactors) [3,5..]
    

    If we demand the next element after 2, we force evaluation of the expression right of the colon. This, in turn, causes filter to evaluate the predicate for 3. Then it goes:

    primeFactors 3
      factor 3 (2 : ...)
        2*2 > 3
      [3]
    [3]
    

    Hence, primeFactors 3 is [3] and we didn’t need to look beyond 2 in primes. (This is the key point why this works!)
    Clearly, the length of [3] is 1 and

    2 : filter ((==1) . length . primeFactors) [3,5..]
    

    evaluates to

    2 : 3 : filter ((==1) . length . primeFactors) [5, 7..]
    

    You may now want to reduce primeFactors 5 for yourself.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.