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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:22:32+00:00 2026-05-11T06:22:32+00:00

I’ve been trying to work my way through Problem 27 of Project Euler, but

  • 0

I’ve been trying to work my way through Problem 27 of Project Euler, but this one seems to be stumping me. Firstly, the code is taking far too long to run (a couple of minutes maybe, on my machine, but more importantly, it’s returning the wrong answer though I really can’t spot anything wrong with the algorithm after looking through it for a while.

Here is my current code for the solution.

/// Checks number for primality. let is_prime n =      [|1 .. 2 .. sqrt_int n|] |> Array.for_all (fun x -> n % x <> 0)  /// Memoizes a function. let memoize f =      let cache = Dictionary<_, _>()     fun x ->          let found, res = cache.TryGetValue(x)         if found then             res         else             let res = f x             cache.[x] <- res             res  /// Problem 27 /// Find a quadratic formula that produces the maximum number of primes for consecutive values of n. let problem27 n =     let is_prime_mem = memoize is_prime     let range = [|-(n - 1) .. n - 1|]     let natural_nums = Seq.init_infinite (fun i -> i)     range |> Array.map (fun a -> (range |> Array.map (fun b ->         let formula n = n * n + a * n + b         let num_conseq_primes = natural_nums |> Seq.map (fun n -> (n, formula n))                                 |> Seq.find (fun (n, f) -> not (is_prime_mem f)) |> fst         (a * b, num_conseq_primes)) |> Array.max_by snd)) |> Array.max_by snd |> fst  printn_any (problem27 1000) 

Any tips on how to a) get this algorithm actually returning the right answer (I think I’m at least taking a workable approach) and b) improve the performance, as it clearly exceeds the ‘one minute rule’ set out in the Project Euler FAQ. I’m a bit of a newbie to functional programming, so any advice on how I might consider the problem with a more functional solution in mind would also be appreciated.

  • 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. 2026-05-11T06:22:33+00:00Added an answer on May 11, 2026 at 6:22 am

    Two remarks:

    1. You may take advantage of the fact that b must be prime. This follows from the fact that the problem asks for the longest sequence of primes for n = 0, 1, 2, ... So, formula(0) must be prime to begin with , but formula(0) = b, therefore, b must be prime.

    2. I am not an F# programmer, but it seems to me that the code does not try n= 0 at all. This, of course, does not meet the problem’s requirement that n must start from 0, therefore there are neglectable chances a correct answer could be produced.

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

Sidebar

Related Questions

No related questions found

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.