5

I'm a first year CS student, with no prior knowledge in programming. I was recommended to go through the problems of Project Euler, and have managed to solve (almost) the first problem.

It states that you have to sum all the numbers which is a multiple of either 3 or 5 (or both).

My code in F#:

let mutable n = 0
for i in 0..1000 do
  if (i % 3 = 0) || (i % 5 = 0) then
    n <- i + n

printfn "%A" n

When this is run, I get answer 234168 which is off by 100 of the real answer.
Any suggestion why?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
VanTheMan
  • 117
  • 6
  • 10
    Hint: the real answer is 233168 which is off by **1000**, not 100. Hint 2: *"Find the sum of all the multiples of 3 or 5 **below** 1000."* – JJJ Dec 21 '16 at 13:11
  • 4
    If you try to solve Project Euler or Project Rosalind in F#, it helps if you try to write more idiomatic code. E.g. `[1..999] |> Seq.filter ( fun x -> x % 3 = 0 || x % 5= 0) |> Seq.sum`\\ – s952163 Dec 21 '16 at 13:21
  • 2
    I would strongly suggest that you learn to code functionally without using `mutable`. By using `mutable` you are taking a step in the wrong direction. When first learning as you are, I would say that it is fine to use `mutable` to get the code working as expected, then rewrite the code to not use `mutable`. When writing the code without `mutable` you can go the path of refactoring, but I would suggest that you take the approach of changing how you think about the program to functionally, think of only a stack for memory, don't think about heap memory. – Guy Coder Dec 21 '16 at 13:47
  • Of Interest: [F#: Recursive Functions: test whether an element is a member of a given list](http://stackoverflow.com/q/35047736/1243762) – Guy Coder Dec 21 '16 at 14:04
  • Of interest: [F#: Recursive Functions: concatenate 2 lists which have common elements](http://stackoverflow.com/q/35098970/1243762) – Guy Coder Dec 21 '16 at 14:05
  • Of interest: [F# Recursive Functions: make list items unique](http://stackoverflow.com/q/35097828/1243762) – Guy Coder Dec 21 '16 at 14:07
  • Of interest: [F#: Recursive Functions: Concatenating the common values between two lists](http://stackoverflow.com/q/35110166/1243762) – Guy Coder Dec 21 '16 at 14:08
  • Of interest: [F#: Recursive functions: Split a list into two equal parts](http://stackoverflow.com/q/35117653/1243762) – Guy Coder Dec 21 '16 at 14:09
  • I tried to do it recursively, like I would with the fibonacci numbers, but I couldn't make it print the correct answer. We haven't been introduced to how to take memory into consideration, when writing code, but it seems as an effective tool, to think of functional. I will definitely look into it. Thanks a lot for the help – VanTheMan Dec 21 '16 at 14:12
  • 3
    You should do well on StackOverflow. You have specific titles for your questions, you explain what you know, explain the problem, give an example of your code, state your problem, don't ramble on, and are receptive to feedback. If only all of the new people here were like this. :) – Guy Coder Dec 21 '16 at 14:43
  • 2
    The hint by JJJ is saying that you should pay attention to `below 1000`, and the comment by  s952163 is using `[1...999]` not `[1...1000]` and JJJ also notes that your answer is off by `1000` which if you stop before `1000` will decrease your answer by `1000`. That is where I would start. – Guy Coder Dec 21 '16 at 14:50
  • If you have the code giving the correct answer then post it as the answer and get yourself some points. It will also give you some credibility here with the F#'ers as they will see that you are contributing back to the community. – Guy Coder Dec 24 '16 at 14:14
  • 1
    so in order to solve this question I actually used s951263's answer of [1..999] |> Seq.filter ( fun x -> x % 3 = 0 || x % 5= 0) |> Seq.sum – VanTheMan Feb 18 '17 at 15:56

1 Answers1

1

Posting an answer to this question to make it easier for others to learn from the mistake.

let mutable n = 0
for i in 0..999 do
  if (i % 3 = 0) || (i % 5 = 0) then
    n <- i + n

printfn "%A" n

Credit should go to JJJ for giving the original hint and Guy Coder for pointing out that a more functional approach would be more idiomatic in F#.

jpierson
  • 16,435
  • 14
  • 105
  • 149