Results 21 to 28 of 28
 10212008, 07:10 AM #21Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
 10212008, 07:28 AM #22Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
ok, here's my code, again:
Java Code:/ import javax.swing.JOptionPane; public class Project7 { /** * @param args the command line arguments */ public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter number"); int n = Integer.parseInt(input); double sum = 1; boolean isPrime = true; for (int i=2; i<=n/2; i++) { if (n % i ==0) { isPrime = false; [COLOR="royalblue"]break; //added this from the textbook[/COLOR] } [COLOR="RoyalBlue"] if(isPrime){ //changed this although it was the least important mistake[/COLOR] sum = sum * (1+(1/(n*n))); [COLOR="RoyalBlue"]JOptionPane.showMessageDialog(null, "The sum at this moment is:" + sum); //added this although it kept spitting out 1.0 the only time Netbeans actually worked right[/COLOR] } } JOptionPane.showMessageDialog(null, "The sum is:" + sum); } }
Last edited by SapphireSpark; 10212008 at 07:53 AM.
 10212008, 02:38 PM #23
Lets look at this code line by line for n = 9
boolean isPrime = true; // assume number is prime
// Let n = 9
for (int i=2; i<=n/2; i++) {
if (n % i ==0) { // n=9 this is NOT true (9 % 2 =1)
isPrime = false; // even division == not prime
break;
// What is value of isPrime here for n =9 first time in loop?
}
if(isPrime){ // here isPrime is still true for 9 % 2
 10212008, 04:03 PM #24Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Ok. And what about the summation part? Does the summation of all the different primes need its own loop?
 10212008, 04:26 PM #25
Yes. One loop to test if a number is prime.(This would be better in a method) The other to add the found prime numbers into the summation.
 10212008, 05:31 PM #26Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Is it a for loop inside the first for?
And i is the variable that becomes the prime number each time, right?
 10212008, 08:19 PM #27
Can you show the code you are talking about?
 10242008, 08:28 AM #28Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Similar Threads

printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10142008, 11:46 PM 
(Help) Fraction Summation and Exponents
By SapphireSpark in forum New To JavaReplies: 19Last Post: 10092008, 04:01 AM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Computing prime numbers in Java
By Java Tip in forum java.langReplies: 0Last Post: 04122008, 08:39 PM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 10:09 AM
Bookmarks