I'm trying to sum all numbers from 1 to 1000 that are either divisible by 3 or 5.
The first attempt is straight forward:
ans1 = 0
for x in 3:999
ans1 += x % 3 == 0 || x % 5 == 0 ? x : 0
end
When I try the same approach using function chaining, it fails to return the answer I expect, it instead returns 0.
ans2 = [3:999] |> x -> x % 3 == 0 || x % 5 == 0 ? x : 0 |> sum
I believe the problem is the center function, since the code below prints all values within the range of 3 to 999. So i know there is no problem with iteration.
[3:999] |> x -> println(x)
Could anyone please help me.
sum([3:3:1000] + sum([5:5:1000]) - sum([15:15:1000])
:-) – Colin T Bowersans3
in my answer turned out to be the best of all the approaches I've taken. But yes, you're exactly right! That is a great solution to the problem, thank you for your comment! :D I must say, Julia is a great language. I started with it yesterday solving Project Euler problems. – Warosaurus