posted 1 year ago
Hello everyone,
As a Java beginner, I decided to tackle the first question from Project Euler's problem set:
https://projecteuler.net/problem=1
In summary, it says:
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.
I've written some code to help me calculate this. See below:
By lowering the multipleLimit value to 10, I can verify that the code provides the correct solution. Something about the double use of the forloop however makes me think there are much better ways to solve this.
I would be grateful if anyone could suggest avenues to pursue in which I could improve the general algorithm or the presented code itself.
My sincere thanks,
Lawrence
As a Java beginner, I decided to tackle the first question from Project Euler's problem set:
https://projecteuler.net/problem=1
In summary, it says:
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.
I've written some code to help me calculate this. See below:
By lowering the multipleLimit value to 10, I can verify that the code provides the correct solution. Something about the double use of the forloop however makes me think there are much better ways to solve this.
I would be grateful if anyone could suggest avenues to pursue in which I could improve the general algorithm or the presented code itself.
My sincere thanks,
Lawrence
posted 1 year ago
My suggestions:
you should be able to do this in one loop rather than two.
The upper bound on your loop goes all the way up to multiple limit.
That considers a whole lot of numbers that are never going to be valid.
Even with a limit of 10, only the first 3 values of 'i' are every going to be useful. From i=4 upwards, multiplying by 3 gives you a value more than 10.
Can you come up with a better loop condition ?
you should be able to do this in one loop rather than two.
The upper bound on your loop goes all the way up to multiple limit.
That considers a whole lot of numbers that are never going to be valid.
Even with a limit of 10, only the first 3 values of 'i' are every going to be useful. From i=4 upwards, multiplying by 3 gives you a value more than 10.
Can you come up with a better loop condition ?
Lawrence King
Greenhorn
Posts: 3
posted 1 year ago
Thank you both  appreciate the quick responses.
Apologies, had to make a change to the code. If I print the following out to the console:
I get multiples < 20 for 5 and 3 respectively. Am I missing the error by printing out just after multipleSum += validMultiple?
I'm going to work on using one loop as suggested.
Many thanks,
Lawrence
Apologies, had to make a change to the code. If I print the following out to the console:
I get multiples < 20 for 5 and 3 respectively. Am I missing the error by printing out just after multipleSum += validMultiple?
I'm going to work on using one loop as suggested.
Many thanks,
Lawrence
posted 1 year ago
The if() on line 15 and line 22 is redundant with the 'while' portion of the for() loop.
The value of '15' is both a multiple of 3 and a multiple of 5, your code will add 15 twice to the sum.
edit: you changed the code out from under me.
The value of '15' is both a multiple of 3 and a multiple of 5, your code will add 15 twice to the sum.
edit: you changed the code out from under me.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Lawrence King
Greenhorn
Posts: 3
Stefan Evans
Bartender
Posts: 1843
10
posted 1 year ago
So yes, just to clarify, if you run it with 20
Multiples of 3: 3,6,9,12,15,18  add up to 63
Multiples of 5: 5, 10, 15  add up to 30
As written originally your program would output 93  because you count 15 as a multiple of both 3 and 5.
Multiples of 3 and 5 < 20 : 3,5,6,9,10,12,15,18 > 78
Multiples of 3: 3,6,9,12,15,18  add up to 63
Multiples of 5: 5, 10, 15  add up to 30
As written originally your program would output 93  because you count 15 as a multiple of both 3 and 5.
Multiples of 3 and 5 < 20 : 3,5,6,9,10,12,15,18 > 78
no wonder he is so sad, he hasn't seen this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
