Euler project number 1. I always get the wrong answer exactly 100, and I don't know why (written inF #)

I am a first-year CS student with no prior knowledge of programming. I was recommended to solve the problems of Project Euler , and they managed to solve the (almost) first problem .

It states that you must sum all numbers that are multiples 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 done, I get a response 234168, which is disabled on 100 real answers.
Any suggestion why?

+5
source share
1 answer

Conducting the answer to this question to make it easier for others to learn from this error.

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

The loan should go to JJJ for providing the original hint and Guy Coder, indicating that a more functional approach would be more idiomatic in F #.

+1
source

Source: https://habr.com/ru/post/1261646/


All Articles