Results 1 to 20 of 21
Thread: Unexpected Output
 01052012, 10:36 AM #1Member
 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[10]; //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; } } 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); } }
While the original answer is: 233168
Please helpLast edited by ankiit; 01052012 at 10:52 AM.
 01052012, 10:44 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Re: Unexpected Output
Take a smaller range and try to add up, following the logic.
 01052012, 10:50 AM #3Member
 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.
 01052012, 10:55 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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.
 01052012, 11:08 AM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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[10]; //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; } }
 01052012, 11:19 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Re: Unexpected Output
What you think now?
 01052012, 11:22 AM #7Member
 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.
Thanks in Advance.
Best Regards,
Ankit
 01052012, 11:23 AM #8Member
 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
 01052012, 11:25 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Re: Unexpected Output
 01052012, 11:26 AM #10
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Re: Unexpected Output
 01062012, 05:06 AM #11Member
 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[10]; //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; } } class AdMulOfThreeFive{ 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); } }
Any help in this regard is welcome.
Thanks in Advance.
Ankit Gupta
 01062012, 05:25 AM #12Member
 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
 01062012, 05:34 AM #13Member
 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; } }
 01062012, 06:42 AM #14
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Re: Unexpected Output
 01062012, 06:45 AM #15
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Re: Unexpected Output
 01062012, 06:51 AM #16
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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.
 01062012, 07:09 AM #17Member
 Join Date
 Dec 2011
 Location
 India
 Posts
 74
 Rep Power
 0
Re: Unexpected Output
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.
With routine Overloading the code 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.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; } }
Ankit
 01062012, 07:38 AM #18
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
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.
 01062012, 07:58 AM #19Member
 Join Date
 Dec 2011
 Location
 India
 Posts
 74
 Rep Power
 0
Re: Unexpected Output
Alright Eranga :) , give me some time.
Thanks for your help :)
 01062012, 08:20 AM #20
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Re: Unexpected Output
You are welcome.
Similar Threads

A basic method with an unexpected output
By JONCOM in forum New To JavaReplies: 6Last Post: 02012011, 07:25 AM 
Error: unexpected type
By silvia in forum New To JavaReplies: 3Last Post: 02052010, 09:54 PM 
unexpected type
By one in forum New To JavaReplies: 13Last Post: 01202009, 09:32 AM 
Unexpected Error
By Mir in forum New To JavaReplies: 10Last Post: 07072008, 07:57 PM 
An unexpected jumper bug on my IO code?
By cruxblack in forum New To JavaReplies: 7Last Post: 07292007, 08:24 AM
Bookmarks