I thought it would be fun to create a thread where the community could solve a problem from Project Euler. This is Problem #1:
If we list all th...
For further actions, you may consider blocking this person and/or reporting abuse
I'll take a mathematical approach.
The sum of the first n positive integers is n*(n + 1)/2. Multiply by k, and you get the sum of the first n multiples of k.
There are 333 multiples of 3 below 1000, and 199 multiples of 5, so we get 166833 and 99500, respectively. But we can't just sum them, as we'd count the multiples of 15 twice (15 is their least common multiple), so we have to subtract those once (total: 33165).
Result: 233168
In general, let's use JavaScript for example:
There, no iterations, pure math 👍 And blazing fast ⚡
Using JavaScript's new support of
BigInt
, it's immediate even with huge numbers like 123456789012345678901234567890: it's 3556368375755728575115582031196717989244271707186887161545.This is very clever and very nice!
yesss the Gaussian sum is such a good tool!
I did this in LOLCODE (first program that I'm writing with it, probably not the best implementation)
👏👏👏👏👏
You can try it here
Answer in C# using LINQ.
Explanation
Enumerable.Range
generates a number between 1 & 1000Where
filters records that matches a condition (It's likefilter
in JS)Sum
is a convenience method for summing up a sequence (instead of doingAggregate((acc, n) => acc + n))
, which is equivalent toreduce
in JS)Source & Tests on Github.
short AND legible!
Thanks Joe :)
Ruby
JavaScript
It's a shame getting a range and a sum isn't quite as easy in JS.
Or generate the range with the es6 spread operator:
There's also a nasty (but shorter)
eval
hack to use in place of reduce. You can use.join(+)
to convert the array into a string containing each number joined by the '+' sign, then evaluate that string as if it's a JavaScript expression to get the sum:It's a bad practice to use eval, of course, but useful for JS code golf.
Or with lodash:
Here it is ¯\_(ツ)_/¯
I'm looking forward for feedbacks
Smallest nitpicking ever: declare
i
inside thefor
loop.It will prevent it from leaking outside the loop.
I wouldn't even call that nitpicking, that is solid best practice advice to avoid memory leaks.
Python
For fun: A shorter solution.
Great solution. Since I lost touch of python it was a little difficult. Now I understand.
Ruby✨💎✨
In Java
Python
or Python One-Liner
Hi, I am adding my first thought on this in Java.
private int sum(int inputValue) {
int sum = 0;
for (int i = 1; i < inputValue; i++) {
if (i % 5 == 0 || i % 3 == 0) {
sum += i;
}
}
return sum;
}
Hi,
I am trying to solve this one, and already solved.
I am going to ask, what is the sum of all the multiples of 3 or 5 below 100000??
I am trying to do this but is giving me a timeout, when using loops.
Python implementation that runs in Θ(1) time
q){sum x where 0=min x mod/:(3,5)}til 1000
k) {+/ &: 0=min x {x-y*x div y}/:(3,5)}(!)1000
or mix and match without lambdas is even shorter and more obfuscated
q)sum (&:) 0=min ((!)1000) mod/: (3,5)
/*Multiples of 3 and 5
Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000. */
include
using namespace std;
int main()
{
int sum=0;
for(int i=1;i<1000;i++)
{
if(i%3==0 ||i%5==0)
{cout<<i<<" ";
sum+=i;}
}
cout<<"\n\n"<<"the sum of all the multiples of 3 or 5 below 1000 = "<<sum<<"\n";
return 0;
}
C++ >> Output >> 233168
Scala:
function multiple(){
var a=Math.trunc(999/3)
var b=Math.trunc(999/5)
var c=Math.trunc(999/15)
var multi3=((3*a)(a+1))/2
var multi5=((5*b)(b+1))/2
var multi15=((15*c)*(c+1))/2
var sum=(multi3+multi5)-multi15
return sum
}
console.log(multiple())
I did that a while ago in C++
Here it is in Go:
github.com/jvarness/euler/blob/mas...
Here's one in Haskell using list comprehension:
Java
I started to solve problems but, well, you know little time so I couldn't continue :) I created a repository at github.
github.com/erhankilic/project-eule...
q)sum (&) 0=min mod/:[; 3 5](!)1000
k) +/&0=min{x-y*x div y}/:[;3 5](!)1000
For a change my k solution is more wordy because the mod function is rather long. Said that, the q above has k in it as well. In pure q it would read
q)sum where 0=min mod/:[;3 5]til 1000
terse and somewhat obfuscated, though the main elements are recognizable, i.e. create the list up to 1000, compute the remainders when dividing by 3 and by 5 and sum across all where either remainder is 0.
Julia:
sum([n for n=1:999 if n%5==0 || n%3==0])