The Artima Developer Community
Sponsored Link

Java Answers Forum
Determine if input number is prime

1 reply on 1 page. Most recent reply: Feb 4, 2003 8:48 AM by tengtium

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Kitty

Posts: 1
Nickname: moxiegirl
Registered: Feb, 2003

Determine if input number is prime Posted: Feb 2, 2003 10:01 AM
Reply to this message Reply
Advertisement
Working on an assignment where I am to determine if the input number is prime or not.To be sure a number has no even divisors besides one and itself, I must test using modulas 2 and then all odd numbers up to its square root. I am to use the Math class square root method, n is number is question:
double sqRoot = Math.sqrt(n)

I am to exit loop as soon as n % any divisor returns 0.

I would appreciate any help I could get to solve this problem. Thanks


tengtium

Posts: 3
Nickname: tengtium
Registered: Feb, 2003

Re: Determine if input number is prime Posted: Feb 4, 2003 8:48 AM
Reply to this message Reply
try this method

/*
process a supplied integer to determine if it is a prime,
if it is, return true, otherwise false
*/

boolean isPrime(int num){ // begin
int limit = Math.sqrt(num);
int denum = 2;
boolean prime = true;

while (denum <= limit) { // begin while
if (number % denum == 0)
prime = false;
denum++;
} // end while
return prime;
} // end

Flat View: This topic has 1 reply on 1 page
Topic: Any tools to analyze JVM Memory. Previous Topic   Next Topic Topic: Connection Closing Problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use