1. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Unexpected Output

Hi Mentors,

I have a problem at hand to find the sum of all the multiples of 3 or 5 below 1000 (Problem 1 - Project Euler)

Here is the code that I have written after hours of thinking but it didn't work out for me:-
Java Code:
```/*An Application to find all the multiples of 3 from range of
* of 1 to 1000 and doing an operation such as addition on the finded valuse
*/

class Threes{

//mul stands for multiple
int mul;
//int stck[]= new int;

//Function findMul finds and return multiples of 3
int findMul(int i)
{
mul = i%3;
if(mul == 0)
{
return i;
}
else
return 0;

}
//Function findMul finds and return multiples of 5
int findMulf(int j)
{
mul = j%5;
if(mul == 0)
{
return j;
}
else
return 0;

}

}

public static void main(String[] args)
{
Threes ob1 = new Threes();
int mltpl,mltplf;
int sum = 0;
int sumf = 0;
int total;
for(int i=0;i<1000;i++ )
{
mltpl = ob1.findMul(i);
if(mltpl > 0)
{
sum  = sum + i;

}
}
for(int j=0;j<1000;j++ )
{
mltplf = ob1.findMulf(j);
if(mltplf > 0)
{
sumf  = sumf + j;

}

}
total = sum+sumf;
System.out.println("The sum is:=" + total);
}

}```
My output is :- 266333
While the original answer is: 233168

Last edited by ankiit; 01-05-2012 at 10:52 AM.  Reply With Quote

2. ## Re: Unexpected Output

Take a smaller range and try to add up, following the logic.  Reply With Quote

3. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output

Hi Eranga,

Thanks a lot for the prompt response, I have tried with the range from 1 to 10 the output is ok with this range , but when I expand the range the output is far away from the answer.

While searching for the correct answer i found it on:-
What is the sum of all the multiples of 3 or 5 below 1000

But i haven't read the method the above mentioned url is explaining.  Reply With Quote

4. ## Re: Unexpected Output

Why don't you try your own. Seems you have pick the logic, but something is missing.

Submit the module value and the range at once to a method and return the answer from it at once.  Reply With Quote

5. ## Re: Unexpected Output

Have a look at the modified code of yours, I have verified your answer.

Java Code:
```public class AdMulOfThreeFive {

public static void main(String[] args) {
Threes ob1 = new Threes();
int mltpl, mltplf;
int sum = 0;
int sumf = 0;
int total;
for (int i = 0; i < 1000; i++) {
mltpl = ob1.findMul(i);
if (mltpl > 0) {
sum = sum + i;

}
}
for (int j = 0; j < 1000; j++) {
mltplf = ob1.findMulf(j);
if (mltplf > 0) {
sumf = sumf + j;

}

}
total = sum + sumf;
System.out.println("The sum is:=" + total);

int iSumOf3 = 0;
int iSumOf5 = 0;

iSumOf3 = ob1.Demo(0, 1000, 3);
iSumOf5 = ob1.Demo(0, 1000, 5);
System.out.println("The sum is:=" + (iSumOf3 + iSumOf5));
}
}

class Threes {

//mul stands for multiple
int mul;
//int stck[]= new int;

//Function findMul finds and return multiples of 3
int findMul(int i) {
mul = i % 3;
if (mul == 0) {
return i;
} else {
return 0;
}

}
//Function findMul finds and return multiples of 5

int findMulf(int j) {
mul = j % 5;
if (mul == 0) {
return j;
} else {
return 0;
}

}

public int Demo(int iMin, int iMax, int iMultiple) {
int iSum = 0;

for (int index = iMin; index < iMax; index++) {
if (index % iMultiple == 0) {
iSum += index;
}
}

return iSum;
}
}```  Reply With Quote

6. ## Re: Unexpected Output

What you think now?  Reply With Quote

7. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output

Hi Eranga,

Thanks a lot for the info.

I think I have to add one more routine to remove duplicate values, like 15 is divisible by both 3 and 5 so it must have been added 2 times.

Best Regards,
Ankit  Reply With Quote

8. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output

Hi Eranga,

Thanks a lot for your help.

I will check this code, and yes I've started using Eclipse, the software very useful.

Thanks  Reply With Quote

9. ## Re: Unexpected Output

Yes it is. The way we implement is not removed the duplicates at all. You can simply avoid that by improving the demo method (if condition) Originally Posted by ankiit Hi Eranga,

Thanks a lot for the info.

I think I have to add one more routine to remove duplicate values, like 15 is divisible by both 3 and 5 so it must have been added 2 times.

Best Regards,
Ankit  Reply With Quote

10. ## Re: Unexpected Output Originally Posted by ankiit Hi Eranga,

Thanks a lot for your help.

I will check this code, and yes I've started using Eclipse, the software very useful.

Thanks
Actually I am using both NetBeans and Eclipse these days. Move to Eclipse, because seems to me that the Eclipse is much easy to use with Android developments.  Reply With Quote

11. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output

Hi,

I think I have optimized some of the parts in my code.
Java Code:
```/*An Application to find all the multiples of 3 or 5 from range of
* of 1 to 1000 and doing an operation such as addition on the finded valuse
*/

class Threes{

//mul stands for multiple
//int mul;							//Not required, saved 4 bytes
//int stck[]= new int;			//Not required saved 40 bytes

//Function findMul finds and return multiples of 3
int findMul(int i)
{
//mul = i%3;
if(i%3 == 0)
{
return i;
}
else
return 0;

}
//Function findMul finds and return multiples of 5
int findMulf(int i)
{
//	mul = i%5;
if(i%5 == 0)
{
return i;
}
else
return 0;

}
//Function findDupl finds and return t dupl values
int findDupl(int i)
{
if(i%3==0 && i%5==0)
return i;
else
return 0;

}

}

public static void main(String[] args)
{
Threes ob1 = new Threes();
/*int mltpl,mltplf,both; */	//used extra 12 bytes of memory, I suppose
int iSumOf3 = 0;
int iSumOf5 = 0;
int iCommInBoth = 0;
int total;
for(int i=0;i<1000;i++ )
{
//mltpl = ob1.findMul(i);
if(ob1.findMul(i) > 0)
{
iSumOf3  += i;

}
//mltplf = ob1.findMulf(i);
if(ob1.findMulf(i) > 0)
{
iSumOf5  +=  i;		//equivalent to sumf=sumf+i
}

//both = ob1.findDupl(i);
if(ob1.findDupl(i) > 0)
{
iCommInBoth += i;
}

}
total = iSumOf3 + iSumOf5 - iCommInBoth;
System.out.println("The sum is:=" + total);
}

}```
The issue I am facing is that when the functions return a value of 0, I have defined nothing for it. What are all the possibilities in this case.

Any help in this regard is welcome.

Ankit Gupta  Reply With Quote

12. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output

Hi Eranga,

Your code is too good :) and it's reduced the lines one has to write for the process.

Thanks a lot for the help.

Is it better to have one function do all the job or small functions each for a specified job?

Thanks
Ankit  Reply With Quote

13. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output

Hi,

The sleak code with fewer lines is as follows:-
Java Code:
```public class AdMultiples {

public static void main(String[] args) {
Threed ob1 = new Threed();

int iSumOf3 = 0;
int iSumOf5 = 0;
int iCommonInBoth = 0;

iSumOf3 = ob1.Demo(0, 1000, 3);
iSumOf5 = ob1.Demo(0, 1000, 5);
iCommonInBoth = ob1.calCommonVal(0, 1000);
System.out.println("The sum is:=" + (iSumOf3 + iSumOf5 - iCommonInBoth));
}
}

class Threed
{

public int Demo(int iMin, int iMax, int iMultiple) {
int iSum = 0;

for (int index = iMin; index < iMax; index++) {
if (index % iMultiple == 0) {
iSum += index;
}
}

return iSum;
}

int calCommonVal(int iMin, int iMax)
{
int iCommon = 0;
for(int index = iMin; index < iMax ; index++)
{
if(index % 3 == 0 && index % 5 == 0)
iCommon += index;
}
return iCommon;
}
}```  Reply With Quote

14. ## Re: Unexpected Output Originally Posted by ankiit The issue I am facing is that when the functions return a value of 0, I have defined nothing for it. What are all the possibilities in this case.

Any help in this regard is welcome.
I am not sure what you problem really is. Mainly your function returns either 0 or the number you are validating. That value you are using for the summation. Zero cannot cause any issues with the summation at all.  Reply With Quote

15. ## Re: Unexpected Output Originally Posted by ankiit Is it better to have one function do all the job or small functions each for a specified job?
It depends on the design you have. However, in general same functionality wrap together (you can deals with technical terms later). Optimizing the code is one of the key challenge. Also it makes your code more readable.  Reply With Quote

16. ## Re: Unexpected Output

Your improved code seems much better. If you can now try to combine both functions into one and return the result at once. Give a try and show it here.  Reply With Quote

17. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output Originally Posted by Eranga Your improved code seems much better. If you can now try to combine both functions into one and return the result at once. Give a try and show it here.
Hi Eranga,

Do we mean routine overloading? because the 2nd routine for finding the duplicates requires two parameters while the routine for finding the multiples requires three paramaters.

Java Code:
```public class AdMultiples {

public static void main(String[] args) {
Threed ob1 = new Threed();

int iSumOf3 = 0;
int iSumOf5 = 0;
int iCommonInBoth = 0;

iSumOf3 = ob1.Demo(0, 1000, 3);
iSumOf5 = ob1.Demo(0, 1000, 5);
iCommonInBoth = ob1.Demo(0, 1000);
System.out.println("The sum is:=" + (iSumOf3 + iSumOf5 - iCommonInBoth));
}
}

class Threed
{
/*function Demo, finds multiples of numbers passed in
* as arguments and also calculates the sum*/
public int Demo(int iMin, int iMax, int iMultiple) {
int iSum = 0;

for (int index = iMin; index < iMax; index++) {
if (index % iMultiple == 0) {
iSum += index;
}
}

return iSum;
}

int Demo(int iMin, int iMax)
{
int iCommon = 0;
for(int index = iMin; index < iMax ; index++)
{
if(index % 3 == 0 && index % 5 == 0)
iCommon += index;
}
return iCommon;
}
}```
Kind Regards
Ankit  Reply With Quote

18. ## Re: Unexpected Output

No, what I mean was using a single function validate that the number is divisible by 3 or 5 or duplicate. Think about it bit.  Reply With Quote

19. Member Join Date
Dec 2011
Location
India
Posts
74
Rep Power
0

## Re: Unexpected Output

Alright Eranga :) , give me some time.  Reply With Quote

20. ## Re: Unexpected Output

You are welcome.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•