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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:31:55+00:00 2026-05-22T15:31:55+00:00

I have two programs to find prime numbers (just an exercise, I’m learning Haskell).

  • 0

I have two programs to find prime numbers (just an exercise, I’m learning Haskell). “primes” is about 10X faster than “primes2”, once compiled with ghc (with flag -O). However, in “primes2”, I thought it would consider only prime numbers for the divisor test, which should be faster than considering odd numbers in “isPrime”, right? What am I missing?

isqrt :: Integral a => a -> a  
isqrt = floor . sqrt . fromIntegral

isPrime :: Integral a => a -> Bool  
isPrime n = length [i | i <- [1,3..(isqrt n)], mod n i == 0] == 1

primes :: Integral a => a -> [a]  
primes n = [2,3,5,7,11,13] ++ (filter (isPrime) [15,17..n])

primes2 :: Integral a => a -> [a]  
primes2 n = 2 : [i | i <- [3,5..n], all ((/= 0) . mod i) (primes2 (isqrt i))]
  • 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-22T15:31:56+00:00Added an answer on May 22, 2026 at 3:31 pm

    I think what’s happening here is that isPrime is a simple loop, whereas primes2 is calling itself recursively — and its recursion pattern looks exponential to me.

    Searching through my old source code, I found this code:

    primes :: [Integer]
    primes = 2 : filter isPrime [3,5..]
    
    isPrime :: Integer -> Bool
    isPrime x = all (\n -> x `mod` n /= 0) $
                takeWhile (\n -> n * n <= x) primes
    

    This tests each possible prime x only against the primes below sqrt(x), using the already generated list of primes. So it probably doesn’t test any given prime more than once.

    Memoization in Haskell:

    Memoization in Haskell is generally explicit, not implicit. The compiler won’t “do the right thing” but it will only do what you tell it to. When you call primes2,

    *Main> primes2 5
    [2,3,5]
    *Main> primes2 10
    [2,3,5,7]
    

    Each time you call the function it calculates all of its results all over again. It has to. Why? Because 1) You didn’t make it save its results, and 2) the answer is different each time you call it.

    In the sample code I gave above, primes is a constant (i.e. it has arity zero) so there’s only one copy of it in memory, and its parts only get evaluated once.

    If you want memoization, you need to have a value with arity zero somewhere in your code.

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

Sidebar

Related Questions

I have two (UNIX) programs A and B that read and write from stdin/stdout.
I have recently been put in charge of debugging two different programs which will
I would like a batch file to launch two separate programs then have the
I have two simple programs set up that share data through a unix domain
I have two simple while loops in my program that I feel ought to
I have a program with two TForm classes and have added a TMainMenu to
Is tight looping in a program bad? I have an application that has two
I have two applications written in Java that communicate with each other using XML
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows

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.