Project Euler

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mathismylove
    New Member
    • Feb 2010
    • 6

    Project Euler

    Hi.

    Im new to Java and attempted to increase my knowledge by working on Project Euler..I figured that If Im able to solve as many problems from the project as possible with Java I will get more familiar with its syntax and the possibilities that lie therein...
    Im in problem no 1

    Problem no 1:

    Find the sum of numbers between 0 and 1000 that are divisible by 3 or 5 and display it


    and here's my code.
    Code:
    mport java.lang.String;
    import java.io.*;
    
    
    public class ProjectEuler {
        
        public static void main(String[] args) {
        	
        int sum, counter, temp;
        	for( counter = 0; counter < 1000; counter ++)
    
              while (((counter % 3) == 0) | ((counter % 5) == 0))
                  {
                    sum = 0;
                    temp = 0;
                    temp = counter;
                    sum = sum + temp;
                  }
    
        System.out.println( "The sum of the numbers is: " +  sum);
    The error that I keep getting is:

    variable sum might not have been initialized
    Last edited by RedSon; Feb 2 '10, 04:07 PM. Reason: Adding [CODE] tags
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    You must put code tags around your code. Please do not forget to do it again.

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      You need to initialize sum outside of the while loop. What happens if counter mod 3 or 5 is never equal to zero?

      Comment

      • Mathismylove
        New Member
        • Feb 2010
        • 6

        #4
        sorry but i don't understand what you mean by putting tags. About your other question. If counter mod 3 or 5 is not equal to zero then counter is not assigned to temp and hence is not included in the sum of the variables. Unless I'm missing something. Thanks.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Read the forum help pages if you don't understand about code tags. As for the other bit, the compiler is telling you that the sum variable needs to be set before entering the loop. You can still reset it in the loop, but the compiler can't figure out exactly what you are doing. All it sees is that you are using a variable that has not been initialized in a loop which may or may not be iterated through.

          Comment

          • Mathismylove
            New Member
            • Feb 2010
            • 6

            #6
            i adjusted my code to have the sum initialized outside the for loop(before it actually!), but still I can't see any output on the output window. Its building and compiling without errors but its not giving me any output...
            Maybe I should create an applet to display the output but I dont even know how to do that. What am I missing?

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              You need to reexamine your loops.

              What happens to your loops when counter is zero?

              Comment

              • Time
                New Member
                • Jan 2010
                • 77

                #8
                Once your counter variable satisfies the while loop condition; no other condition provided for the loop to break; come out of itself.

                Comment

                • Mathismylove
                  New Member
                  • Feb 2010
                  • 6

                  #9
                  interesting insights guys..thanks redson and time...im on it!

                  Comment

                  Working...