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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:50:13+00:00 2026-05-25T11:50:13+00:00

I have this code written for a Project Euler problem in c++: int sum

  • 0

I have this code written for a Project Euler problem in c++:

int sum = 0;

for(int i =0; i < 1000; i++)
{
    //Check if multiple of 3 but not multiple of 5 to prevent duplicate
    sum += i % 3 == 0 && i % 5 != 0 ? i: 0;
    //check for all multiple of 5, including those of 3
    sum += i % 5 == 0 ? i: 0;
}
    cout << sum;

I’m trying to learn f# and rewriting this in f#. This is what I have so far:

open System

//function to calculate the multiples
let multiple3v5 num =
    num

//function to calculate sum of list items
let rec SumList xs =
    match xs with
    | []    -> 0
    | y::ys -> y + SumList ys

let sum = Array.map multiple3v5 [|1 .. 1000|]

What I have may be complete nonsense…so help please?

  • 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-25T11:50:13+00:00Added an answer on May 25, 2026 at 11:50 am

    Your sumList function is a good start. It already iterates (recursively) over the entire list, so you don’t need to wrap it in an additional Array.map. You just need to extend your sumList so that it adds the number only when it matches the specified condition.

    Here is a solution to a simplified problem – add all numbers that are divisible by 3:

    open System
    
    let rec sumList xs =
      match xs with
      | []    -> 0 // If the list is empty, the sum is zero
      | y::ys when y % 3 = 0 -> 
         // If the list starts with y that is divisible by 3, then we add 'y' to the
         // sum that we get by recursively processing the rest of the list
         y + sumList ys
      | y::ys -> 
         // This will only execute when y is not divisible by 3, so we just
         // recursively process the rest of the list and return 
         /// that (without adding current value)
         sumList ys
    
    // For testing, let's sum all numbers divisble by 3 between 1 and 10.
    let sum = sumList [ 1 .. 10 ]
    

    This is the basic way of writing the function using explicit recursion. In practice, the solution by jpalmer is how I’d solve it too, but it is useful to write a few recursive functions yourself if you’re learning F#.

    The accumulator parameter mentioned by sashang is a more advanced way to write this. You’ll need to do that if you want to run the function on large inputs (which is likely the case in Euler problem). When using accumulator parameter, the function can be written using tail recursion, so it avoids stack overflow even when processing long lists.

    The idea of a accumulator-based version is that the function takes additional parameter, which represents the sum calculated so far.

    let rec sumList xs sumSoFar = ...
    

    When you call it initially, you write sumList [ ... ] 0. The recursive calls will not call y + sumList xs, but will instead add y to the accumulator and then make the recursive call sumList xs (y + sumSoFar). This way, the F# compiler can do tail-call optimization and it will translate code to a loop (similar to the C++ version).

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

Sidebar

Related Questions

I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have this code in my ASP.NET application written in C# that is trying
In our application, I have seen code written like this: User.java (User entity) public
This is similar to this question, except the code is already written. I have
I have always written my boolean expressions like this: if (!isValid) { // code
I have this code that uses the USE_CONVERSION macro in a C++ project... I
I have to modify a project written by someone else. Because the code is
I have lots of Java code written by other people and not residing in

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.