Computing.Net > Forums > Programming > Help with loops

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with loops

Reply to Message Icon

Name: haleyrt
Date: October 3, 2009 at 12:43:49 Pacific
OS: Windows Vista
Subcategory: Java
Tags: loop, prime numbers, rsa encryption
Comment:

having a little trouble with my code this is for a class so maybe more suggestion help then direct unless its a small mistake thank you!
im making a loop to generate a random number between 2-128 then trying to find if its prime

int p;
int function;
boolean isPrime;
double formattedP;

while(isPrime= false)
formattedP = Math.random() * 126 + 2;
p = (int) formattedP;

isPrime = true;
for (int i = 2; i < p; i++) {

function = p % i;
if (function == 0) {
isPrime = false;
}
}
System.out.println(p + " " + isPrime);}

its telling me fomattedP is never intilized when tyring to plug it into p and casting it to an int



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 3, 2009 at 12:48:55 Pacific
Reply:

Then let's reformat your code to make the problem clearer.

isPrime = false;
while(false) {
  formattedP = Math.random() * 126 + 2;
}

See it yet?

0

Response Number 2
Name: haleyrt
Date: October 3, 2009 at 13:04:25 Pacific
Reply:

if i put curly brackets around formattedP it still tells me it is never intilazied when it comes to casting it into p
i do know that what is wrong is very small haha sigh

int p;
int function;
boolean isPrime = false;
double formattedP;

while(isPrime = false)
formattedP = Math.random() * 126 + 2;
p = (int) formattedP;

isPrime = true;
for (int i = 2; i < p; i++) {

function = p % i;
if (function == 0) {
isPrime = false;
}
}
System.out.println(p + " " + isPrime);}}


0

Response Number 3
Name: xirsteon
Date: October 3, 2009 at 13:34:00 Pacific
Reply:

I haven't test this as I do not have any c/java compilers installed but how about you try casting formattedP when you generate the random values?

i.e.

while(isPrime = false)
(int) formattedP = Math.random() * 126 + 2;
out.println(formattedP); // print formattedP to see what it looks like
p = formattedP;

> Option 2


while(isPrime = false)
formattedP = (int)Math.random() * 126 + 2;
out.println(formattedP); // print formattedP to see what it looks like
p = formattedP;


Try both options but i'm more confident with option 2.


-1

Response Number 4
Name: Razor2.3
Date: October 3, 2009 at 13:47:43 Pacific
Reply:

haleyrt: i put curly brackets around formattedP
Oh, you misunderstand. I'm pointing out what you did, not telling you what you need to do.

Here's what your code compiles down to:

double formattedP;
isPrime= false;
p = (int) formattedP;


0

Response Number 5
Name: haleyrt
Date: October 3, 2009 at 13:49:41 Pacific
Reply:

so its not putting in the random interger? why is that?


0

Related Posts

See More



Response Number 6
Name: Razor2.3
Date: October 3, 2009 at 13:59:46 Pacific
Reply:

Because while (false) will never run. The language does the loop test before running the while loop, and the test is false.


0

Response Number 7
Name: haleyrt
Date: October 3, 2009 at 14:06:05 Pacific
Reply:

so how do i make a loop that runs my code until it finds a prime number?


0

Response Number 8
Name: Razor2.3
Date: October 3, 2009 at 14:54:09 Pacific
Reply:

Initialize your boolean earlier in your code, change your test to test for not isPrime, and wrap everything in the function below the while line in braces (otherwise you'll just end up in an endless loop). Also, fix your prime check, or your code will always say the number is prime.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Help with loops

Help with a C Program / Newbie www.computing.net/answers/programming/help-with-a-c-program-newbie/8071.html

Help with debugging C++ code www.computing.net/answers/programming/help-with-debugging-c-code/13083.html

Help with loop www.computing.net/answers/programming/help-with-loop/15358.html