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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:57:16+00:00 2026-05-13T06:57:16+00:00

I am trying to do problem 12 in Project Euler. numDivisor64 is to calculate

  • 0

I am trying to do problem 12 in Project Euler.

numDivisor64 is to calculate number of divisors.

I wrote this F# code:

let problem12 =
    {1L..300000L} |> Seq.map (fun x->x*(x+1L)/2L) |> Seq.map numDivisor64 |> Seq.filter (fun x->x>500L)

The problem asks to find the number rather than its # of divisors. Besides writing this in a less compact way using loops or recursion, any beautiful method?

Another problem, I occasionally find that I need to convert a 32-bit int version of code to a 64-bit one by adding ‘L’ to all the numbers. Is there a way to avoid this? Anything like c++ template?

I first wrote

let numDivisor n =
    let rec countd n d =
        if n%d=0 then
            let n2, cnt = countd (n/d) d 
            n2, cnt+1
        else
            n, 0

    let rec collect n d = 
        if n < d then 1
        elif n%d=0 then
            let n2, cnt = countd n d
            (cnt+1) * (collect n2 d)
        else
            collect n (d+1)
    collect n 2

Later I found I need bigger integers:

let numDivisor64 n =
    let rec countd n d =
        if n%d=0L then
            let n2, cnt = countd (n/d) d 
            n2, cnt+1L
        else
            n, 0L

    let rec collect n d = 
        if n < d then 1L
        elif n%d=0L then
            let n2, cnt = countd n d
            (cnt+1L) * (collect n2 d)
        else
            collect n (d+1L)
    collect n 2L
  • 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-13T06:57:16+00:00Added an answer on May 13, 2026 at 6:57 am

    I would rephrase the search for the first wanted number as follows:

    • start with an infinite stream of int64’s

    • turn them into triangle numbers

    • find the first number that satisfies the condition (instead of mapping to the number of divisors, which is not what you want, you want the number itself).

    code:

    let problem12 =
        Seq.initInfinite int64 //the same as Seq.initInfinite (fun n -> int64 n)
        |> Seq.map (fun x -> x*(x+1L)/2L)
        |> Seq.find (fun x -> numDivisor64 x > 500L)
    

    Regarding the second problem: when I solve project Euler problems I usually use int64’s by default, because of type inference restrictions.

    It’s possible to write a more generic version using the inline keyword. See for instance this thread over at hubFS.

    EDIT: here’s a more generic version, using the technique described in the above link:
    The type signature of NumDivisorG becomes horrible, but should work for any data type that ‘knows’ *,+,1 and 0.

    module NumericLiteralG =
      let inline FromZero() = LanguagePrimitives.GenericZero
      let inline FromOne() = LanguagePrimitives.GenericOne
    
    let inline numDivisorG n =
        let rec countd n d =
            if n%d=0G then
                let n2, cnt = countd (n/d) d 
                n2, cnt+1G
            else
                n, 0G
    
        let rec collect n d = 
            if n < d then 1G
            elif n%d=0G then
                let n2, cnt = countd n d
                (cnt+1G) * (collect n2 d)
            else
                collect n (d+1G)
        collect n (1G+1G)
    
    let problem12L =
        Seq.initInfinite int64 //the same as Seq.initInfinite (fun n -> int64 n)
        |> Seq.map (fun x -> x*(x+1L)/2L)
        |> Seq.find (fun x -> numDivisorG x > 500L)
    
    let problem12I =
        Seq.initInfinite id //the same as Seq.initInfinite (fun n -> n)
        |> Seq.map (fun x -> x*(x+1)/2)
        |> Seq.find (fun x -> numDivisorG x > 500)    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am trying to do problem 254 in project euler and arrived at this
I'm trying to solve Problem #5 in Project Euler. The code works for the
I'm trying to work through Project Euler and I'm hitting a barrier on problem
I'm trying to solve Problem 41 of project Euler in Java, by counting the
I'm trying to solve problem 50 on Project Euler . Don't give me the
I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I
I'm trying to complete the Project Euler problem found here. For some reason my
I've been trying to work my way through Problem 27 of Project Euler, but
I'm trying to write a Rubyish solution to problem 6 in Project Euler, because
While trying problem 41 from the Euler Project, I ran across what seems to

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.