Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 26th, 2013, 03:24 AM
Authorized User
 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unsure of how code works: Example: Primes

/**I've left the programmers comments in. What I am having trouble with is
the part of the code that reads (below this comment block):

If(i % j == 0) {
isPrime = false;
break;
}
What I am having trouble with is that both i and j are the same value incremented each time they run...I think...so wouldn't that mean that the value of i % j would always be zero because it is the same value divided by another? What am I missing? And then also, if there is no remainder wouldn't that indicate that the number is prime? I know there is something I am missing...Please help me out. Thank you. :)

*/


public class Primes {
public static void main(String[] args) {
int nValues = 50; // The maximum value to be checked
boolean isPrime = true; // Is true if we find a prime

// Check all values from 2 to nValues
for(int i = 2; i <= nValues; ++i) {
isPrime=true; // Assume the current i is prime

// Try dividing by all integers from 2 to i-1
for(int j = 2; j < i; ++j) {
if(i % j == 0) { // This is true if j divides exactly
isPrime = false; // If we got here, it was an exact division
break; // so exit the loop
}
}
// We can get here through the break, or through completing the loop
if(isPrime) // So is it prime?
System.out.println(i); // Yes, so output the value
}
}
}
/**
The outputs in the book are:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47


*/

Last edited by alex001; March 26th, 2013 at 04:36 AM..
 
Old April 26th, 2013, 05:53 PM
Registered User
 
Join Date: Apr 2013
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default Prime Number example

Let's run through a few cycles of the outer and inner loops:

Iteration 1 - outer loop
isPrime = true // reset isPrime to true
i = 2
Iteration 1 - inner Loop
for (int j = 2 ; j < i; ++j) // false: 2 < 2
// inner loop is not executed
if(isPrime) // true: isPrime = true
System.out.println(i); // 2 is a prime number
// back to beginning of outer loop

Iteration 2 - outer loop
isPrime = true // reset isPrime to true
i = 3
Iteration 1 - inner Loop
for (int j = 2 ; j < i; ++j) // true: 2 < 3
// j gets incremented to 3
if(i % j == 0) { // false: 3 % 2 == 0
// fall thru to end of inner loop
// back to beginning of inner loop
Iteration 2 - inner Loop
j = 3 // j is now 3
for (int j = 2 ; j < i; ++j) // false: 3 < 3
// inner loop is not executed
if(isPrime) // true: isPrime = true
System.out.println(i); // 3 is a prime number
// back to beginning of outer loop

Iteration 3 - outer loop
isPrime = true // reset isPrime to true
i = 4
Iteration 1 - inner Loop
for (int j = 2 ; j < i; ++j) // true: 2 < 4
// j gets incremented to 3
if(i % j == 0) // true: 4 % 2 == 0
isPrime = false; // isPrime is now false
break; // exit from inner loop
if(isPrime) // false: isPrime = false
// back to beginning of outer loop

Iteration 4 - outer loop
isPrime = true // reset isPrime to true
i = 5
Iteration 1 - inner Loop
for (int j = 2 ; j < i; ++j) // true: 2 < 5
// j gets incremented to 3
if(i % j == 0) // false: 5 % 2 == 0
// fall thru to end of inner loop
// back to beginning of inner loop
Iteration 2 - inner Loop
j = 3 // j is now 3
for (int j = 2 ; j < i; ++j) // true: 3 < 5
// j gets incremented to 4
if(i % j == 0) // false: 5 % 3 == 0
// fall thru to end of inner loop
// back to beginning of inner loop
Iteration 3 - inner Loop
j = 4
for (int j = 2 ; j < i; ++j) // true: 4 < 5
// j gets incremented to 5
if(i % j == 0) // false: 5 % 4 == 0
// fall thru to end of inner loop
// back to beginning of inner loop
Iteration 4 - inner Loop
j = 5
for (int j = 2 ; j < i; ++j) // false: 5 < 5
// inner loop is not executed
if(isPrime) // true: isPrime = true
System.out.println(i); // 5 is a prime number
// back to beginning of outer loop

---------------------------
Regarding your second question:
A number is a prime number only if it is divisible by 1 or itself.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Code Works One Way but Not Another When Testing For Values ritagr XSLT 2 March 22nd, 2013 12:55 PM
Source code which really works bill x. BOOK: Professional PHP Design Patterns 0 February 4th, 2013 11:10 PM
Unsure how to work with the Code examples leemark2k3 BOOK: Professional ASP.NET 4 : in C# and VB 3 March 6th, 2012 05:01 PM
Code that works !! for chapter 3 dawsonje44 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 March 2nd, 2007 04:33 PM
code works in IE but not netscape Toka1 Javascript How-To 2 November 27th, 2003 05:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.