# [Erlang] Primality Checker

Page 1 of 1

## 0 Replies - 1005 Views - Last Post: 15 July 2010 - 07:35 AM

### #1 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12211
• Posts: 45,290
• Joined: 27-December 08

# [Erlang] Primality Checker

Posted 15 July 2010 - 07:35 AM

Description: Invoke prime:is_prime(number) to determine it's primality.Checks for the Primality of a Number
```-module(prime).
-export([is_prime/1]).

%the entry function, starts at N mod 2
is_prime(N) -> check_prime(N,2).

%case 1: base case- nothing after is prime
check_prime(N,K) when K >= N/2 -> N rem K /= 0;

%case 2: If we can't disprove primality, go to the next number
check_prime(N,K) when N rem K > 0 -> check_prime(N,K+1);

%case 3: If we can disprove primality, stop.
check_prime(N,K) when N rem K == 0 -> false.

```

Is This A Good Question/Topic? 0

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; }