# Check to See If a Number is Prime

Page 1 of 1

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

### #1 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12646
• Posts: 45,819
• 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

• ﻿>>──(Knee)──►

Reputation: 127
• 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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }