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

#1 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12211
  • View blog
  • 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