1 Replies - 2250 Views - Last Post: 10 November 2011 - 10:18 AM

#1 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12641
  • View blog
  • Posts: 45,813
  • Joined: 27-December 08

Check to See If a Number is Prime

Posted 23 November 2009 - 03:48 PM

Description: Call the method, using the number to be checked as the parameter.Checks number for primality through a simple iterative approach.
public boolean isPrime(int n){
    for(int i = 2; i <= Math.sqrt(n); i += 2){
            if(n%i == 0) return false;
    }
    return true;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Check to See If a Number is Prime

#2 RetardedGenius   User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 127
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Check to See If a Number is Prime

Posted 10 November 2011 - 10:18 AM

This is a very inefficient primality test (even for a deterministic one), furthermore the 'n != i' expression is redundant.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1