Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: Unexpected Output

  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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);	
    	}
    		
    }
    My output is :- 266333
    While the original answer is: 233168

    Please help
    Last edited by ankiit; 01-05-2012 at 11:52 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default 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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default 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;
        }
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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

  8. #8
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default 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)

    Quote Originally Posted by ankiit View Post
    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

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Unexpected Output

    Quote Originally Posted by ankiit View Post
    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.

  11. #11
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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);	
    	}
    		
    }
    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.

    Thanks in Advance.
    Ankit Gupta

  12. #12
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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

  13. #13
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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;
    	}
    }

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Unexpected Output

    Quote Originally Posted by ankiit View Post
    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.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Unexpected Output

    Quote Originally Posted by ankiit View Post
    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.
    ankiit likes this.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default 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.

  17. #17
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Unexpected Output

    Quote Originally Posted by Eranga View Post
    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.

    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;
    	}
    }
    Kind Regards
    Ankit

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default 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.

  19. #19
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Unexpected Output

    Alright Eranga :) , give me some time.

    Thanks for your help :)

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Page 1 of 2 12 LastLast

Similar Threads

  1. A basic method with an unexpected output
    By JONCOM in forum New To Java
    Replies: 6
    Last Post: 02-01-2011, 08:25 AM
  2. Error: unexpected type
    By silvia in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 10:54 PM
  3. unexpected type
    By one in forum New To Java
    Replies: 13
    Last Post: 01-20-2009, 10:32 AM
  4. Unexpected Error
    By Mir in forum New To Java
    Replies: 10
    Last Post: 07-07-2008, 08:57 PM
  5. An unexpected jumper bug on my IO code?
    By cruxblack in forum New To Java
    Replies: 7
    Last Post: 07-29-2007, 09:24 AM

Posting Permissions

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