Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 missing bracket
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Mon Jun 12, 2006 8:10 pm   Post subject: missing bracket

i made this little program but it won't run and it says its missing a bracket on line 13 and i'm looking at it but it seems right to me. any suggestions?

code:

public class problem10 {
        public static void main (String[] args) {
                int total = 17;

                for(int i = 8; i <= 999999; i++){
            if ((i mod 2 == 0) || (i mod 3 == 0) || (i mod 5 == 0) || (i mod 7 == 0)){
                total = total;
            }
            else{
                total = total + i;
            }
            System.out.println(total);
            }
        }
}
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Mon Jun 12, 2006 8:26 pm   Post subject: (No subject)

umm "mod" is "%" in java. The compiler thinks your wrote a function but forgot to open-close the brackets.
cool dude




PostPosted: Mon Jun 12, 2006 8:41 pm   Post subject: (No subject)

ahhhhhhhh Embarassed oh why did i learn turing!!! i actually knew that thanx for pointing it out. well look at the bright side i will never do that again (hopefully)
cool dude




PostPosted: Mon Jun 12, 2006 9:05 pm   Post subject: (No subject)

k i'm doing something wrong, because the problem says:

Quote:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below one million.



now i'm getting some negative answer because i'm getting a really big number. i'm pretty sure its with my if statement determining if its a prime number or not.
HellblazerX




PostPosted: Tue Jun 13, 2006 4:59 pm   Post subject: (No subject)

Double check your calculations again. The numbers shouldn't become negative when they become too high. Also, if your numbers really are getting that high, then you should check out the BigInteger class.
cool dude




PostPosted: Tue Jun 13, 2006 5:06 pm   Post subject: (No subject)

HellblazerX wrote:
Double check your calculations again. The numbers shouldn't become negative when they become too high. Also, if your numbers really are getting that high, then you should check out the BigInteger class.


technically they're not suppused to be that high, but i know i screwed something up. thats the right concept to check for a prime number right?
HellblazerX




PostPosted: Tue Jun 13, 2006 5:11 pm   Post subject: (No subject)

Well, you'll get most of them. The only ones you'll be forgetting are the the exponents of prime numbers. i.e., 169 and 2197, which are 13 squared and 13 cubed respectively, they're not prime numbers, but they won't be recognized by your is statement. I guess one way to get those numbers is to store the prime numbers you've already found, and include them in your if statement, somehow.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: