0

I'm new to Erlang and just had a question. I've already looked here on StackOverflow and done a lot of Googling.

I'm trying to write a function that takes two parameters and returns the prime numbers between them. My biggest issue is with Prime testing (checking if a number is prime). I fix the rest of the stuff later.

Here's the code I have so far:

-module(ListPrime). -export([primeList/2]).

primeList(0, 0)-> io:format("No prime numbers here ~s~n", [0]); `

primeList(Start, Finish)-> CheckPrime = Finish rem Start, if Start =< Finish, CheckPrime == 1 -> primeList(Start, Finish-1) end.

Basically what I'm trying to do is:

  • Check if Finish is a prime number.

  • If not, move on to the next number (Finish-1).

  • Continue until the base case has been reached.

It compiles but it obviously doesn't do what I want it to do because I don't know how to check if a number is prime.

I know what the definition of a Prime Number is (a number that is only divisible by itself and 1) but the only thing that comes to mind to write when I think about that definition is:

Finish rem Finish

and line of code works for any number that is used. How do I check if a number is prime in Erlang? Thank you very much.

red_herring
  • 11
  • 1
  • 3
  • There is a discussion about this in SICP that translates readily to Erlang. https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.6 Testing for primality is an active area of research. – zxq9 Sep 17 '15 at 06:16
  • Look [here](http://stackoverflow.com/a/10249801/448810). – user448810 Sep 17 '15 at 12:39

5 Answers5

0

The code below will test if a number is prime. just call the function isPrime and it will return true or false.

-module(isPrime).
-export([isPrime/1]).

 isPrime(0)-> false;
 isPrime(1)-> false;
 isPrime(2)-> true;


isPrime(N)->
  ChPrime = N rem 2,
  if
    ChPrime == 1 -> false;
    ChPrime =:= 1 -> true
end.
byaruhaf
  • 4,128
  • 2
  • 32
  • 50
  • This seems just to check divisibility by two, and knows the values of the test for a few numbers. The if statement with the two different equality operators makes no much sense to me. – mvw Jun 10 '17 at 20:44
0

try this, I hope it workds fine, just call the function prime and it will return true of false :

divisors(N) ->
    [ X || X <- lists:seq(1,N), (N rem X)==0].
prime(N) when N == 0; N == 1 ->
    false;
prime(2) ->
    true;
prime(N) ->
    divisors(N) == [1,N].
Salhi Fadhel
  • 15
  • 11
  • This might work. It is simple, the clause for two seems to be not needed. There are faster algorithms. – mvw Jun 10 '17 at 20:48
0

Try this:

prime([X|_]) when X =< 1 -> error(not_a_prime);
prime([X|Ns]) -> [N || N <- Ns, N rem X /= 0].
ghnome
  • 57
  • 1
  • 7
0

You need a caller function! Just do it like this:

%% caller function
run(X, Y) ->
    prime(lists:seq(X, Y)).

%% prime function
prime([X|_]) when X =< 1 -> error(not_a_prime);
prime([X|Ns]) -> [N || N <- Ns, N rem X /= 0].

lists:seq(X, Y) := creates a list between two numbers

ghnome
  • 57
  • 1
  • 7
-1

This may not be the most efficient way to set the function, but it works:

prime(2)->true;
prime(N)when N rem 2 =:= 0-> false;
prime(3)->true;
prime(Odd)->prime(Odd,3).

prime(N,I)when N rem I =:= 0->false;
prime(N,I)when I*I > N->true;
prime(N,I)->prime(N,I+2).

primes(Start,Finish)->[X|| X <- lists:seq(Start,Finish), prime(X)].