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

  • Home
  • SEARCH
  • 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 8295867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:45:30+00:00 2026-06-08T14:45:30+00:00

I have the following solution in Haskell to Problem 3 : isPrime :: Integer

  • 0

I have the following solution in Haskell to Problem 3:

isPrime :: Integer -> Bool
isPrime p = (divisors p) == [1, p]

divisors :: Integer -> [Integer]
divisors n = [d | d <- [1..n], n `mod` d == 0]

main = print (head (filter isPrime (filter ((==0) . (n `mod`)) [n-1,n-2..])))
  where n = 600851475143

However, it takes more than the minute limit given by Project Euler. So how do I analyze the time complexity of my code to determine where I need to make changes?

Note: Please do not post alternative algorithms. I want to figure those out on my own. For now I just want to analyse the code I have and look for ways to improve it. Thanks!

  • 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-06-08T14:45:32+00:00Added an answer on June 8, 2026 at 2:45 pm

    Let’s start from the top.

    divisors :: Integer -> [Integer]
    divisors n = [d | d <- [1..n], n `mod` d == 0]
    

    For now, let’s assume that certain things are cheap: incrementing numbers is O(1), doing mod operations is O(1), and comparisons with 0 are O(1). (These are false assumptions, but what the heck.) The divisors function loops over all numbers from 1 to n, and does an O(1) operation on each number, so computing the complete output is O(n). Notice that here when we say O(n), n is the input number, not the size of the input! Since it takes m=log(n) bits to store n, this function takes O(2^m) time in the size of the input to produce a complete answer. I’ll use n and m consistently to mean the input number and input size below.

    isPrime :: Integer -> Bool
    isPrime p = (divisors p) == [1, p]
    

    In the worst case, p is prime, which forces divisors to produce its whole output. Comparison to a list of statically-known length is O(1), so this is dominated by the call to divisors. O(n), O(2^m)

    Your main function does a bunch of things at once, so let’s break down subexpressions a bit.

    filter ((==0) . (n `mod`))
    

    This loops over a list, and does an O(1) operation on each element. This is O(m), where here m is the length of the input list.

    filter isPrime
    

    Loops over a list, doing O(n) work on each element, where here n is the largest number in the list. If the list happens to be n elements long (as it is in your case), this means this is O(n*n) work, or O(2^m*2^m) = O(4^m) work (as above, this analysis is for the case where it produces its entire list).

    print . head
    

    Tiny bits of work. Let’s call it O(m) for the printing part.

    main = print (head (filter isPrime (filter ((==0) . (n `mod`)) [n-1,n-2..])))
    

    Considering all the subexpressions above, the filter isPrime bit is clearly the dominating factor. O(4^m), O(n^2)

    Now, there’s one final subtlety to consider: throughout the analysis above, I’ve consistently made the assumption that each function/subexpression was forced to produce its entire output. As we can see in main, this probably isn’t true: we call head, which only forces a little bit of the list. However, if the input number itself isn’t prime, we know for sure that we must look through at least half the list: there will certainly be no divisors between n/2 and n. So, at best, we cut our work in half — which has no effect on the asymptotic cost.

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

Sidebar

Related Questions

I have trouble finding a solution for the following problem: I have a lot
I have the following (correct) solution to Project Euler problem 24. I'm relatively new
I have the following Solution: SomeProject.Ria (non Silverlight code) SomeProject.Ria.Silverlight (Silverlight light code, namespace
I currently have the following in one solution: Core Project (data access, biz logic,
When using ASP.net webforms my usual solution would have following type of setup -
I have the following code in a full .NET framework solution: public delegate int
I have the following question, and it screams at me for a solution with
gem install heroku failed with following message and I have tried the solution here
Can someone recommend a hosted solution that answers the following requirements (I have seen
I'm forced to find a solution for the following situation: I have a h1

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.