patternjavaModerate
Project Euler 1 in Java
Viewed 0 times
projecteulerjava
Problem
I have following exercise:
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.
So I wrote below code which finds the sum of all multiples of 3 or 5 below 1000.
Main.java:
How it code works?
I declared sum variable, iterated through i = 1 to i = 999 and if i is multiply of 3 or 5 then I add it to the sum. Finally, I printed out the sum variable.
Questions
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.
So I wrote below code which finds the sum of all multiples of 3 or 5 below 1000.
Main.java:
package pl.hubot.projecteuler.problem1;
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) sum += i;
}
System.out.print(sum);
}
}How it code works?
I declared sum variable, iterated through i = 1 to i = 999 and if i is multiply of 3 or 5 then I add it to the sum. Finally, I printed out the sum variable.
Questions
- Is it a efficient method to solve this exercise?
- How can I simplify code?
- How can I increase performance of this code?
Solution
I think it is very concise, well done!
For this small problem this will be fast enough.
Code only
I would only add braces around the
For performance, please note that modulo is kind of slow. You could implement two variables that hold multiples of 3 and 5.
Math
Better will be to go purely mathemathical:
The formula for the sum of an arithmetic series is:
Sn = (n/2) * (a1 + an)
Sn = the sum of the n terms in the sequence.
a1 = the first term in the sequence.
an = the nth term in the sequence.
We need to determine the number of items 'n' in the sequence. For simplicity I name this
This is calculated as dividing the
The number an is the last multiple that fits below the limit, which is equal to
Calculate the sum om the series for threes, fives and fifteens. The solution is:
In code:
For this small problem this will be fast enough.
Code only
I would only add braces around the
if body:for (int i = 1; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}For performance, please note that modulo is kind of slow. You could implement two variables that hold multiples of 3 and 5.
public static void main(String[] args) {
int threes = 0;
int fives = 0;
int sum = 0;
for (int i = 1; i threes)
threes+=3;
if (i>fives)
fives+=5;
if (i == threes || i == fives )
sum+=i;
}
System.out.print(sum);
}Math
Better will be to go purely mathemathical:
The formula for the sum of an arithmetic series is:
Sn = (n/2) * (a1 + an)
Sn = the sum of the n terms in the sequence.
a1 = the first term in the sequence.
an = the nth term in the sequence.
We need to determine the number of items 'n' in the sequence. For simplicity I name this
occurrences. This is calculated as dividing the
limit - 1 by number. Because the integer division, it is rounded down.The number an is the last multiple that fits below the limit, which is equal to
occurrences * numberCalculate the sum om the series for threes, fives and fifteens. The solution is:
Sum(threes) + Sum(fives) - Sum(fifteens)In code:
public static int sum( int limit, int number )
{
int occurrences = (limit-1) / number;
//Below is a rewrite ( thnx to Olivier Grégoire) of:
// (int) ( ( occurrences / 2.0 ) * ( occurrences * number + number) );
return number * occurrences * (occurrences + 1) / 2;
}
public static void main( String[] args )
{
System.out.println( sum(1000,3) + sum(1000,5) - sum(1000, 15));
}Code Snippets
for (int i = 1; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}public static void main(String[] args) {
int threes = 0;
int fives = 0;
int sum = 0;
for (int i = 1; i < 1000; i++) {
if (i>threes)
threes+=3;
if (i>fives)
fives+=5;
if (i == threes || i == fives )
sum+=i;
}
System.out.print(sum);
}Sum(threes) + Sum(fives) - Sum(fifteens)public static int sum( int limit, int number )
{
int occurrences = (limit-1) / number;
//Below is a rewrite ( thnx to Olivier Grégoire) of:
// (int) ( ( occurrences / 2.0 ) * ( occurrences * number + number) );
return number * occurrences * (occurrences + 1) / 2;
}
public static void main( String[] args )
{
System.out.println( sum(1000,3) + sum(1000,5) - sum(1000, 15));
}Context
StackExchange Code Review Q#162379, answer score: 10
Revisions (0)
No revisions yet.