-
Notifications
You must be signed in to change notification settings - Fork 0
/
mersenneprimes.m
36 lines (28 loc) · 916 Bytes
/
mersenneprimes.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
% Find first 8 Mersenne numbers
%
% A Mersenne number is a number of the form ...
% M(n)= 2^n - 1 where n is an integer.
%
% A mersenne prime is a prime number of the form ...
% M(p) = 2^p -1 where p is a prime number.
clc ; clear ;
mersenne_primes = find_mersenne_primes() ;
function mersenne_primes = find_mersenne_primes()
count = 0;
n = 2;
mersenne_primes = [];
prime_nums = [] ;
fprintf('%5s %5s %10s %15s\n', 'Rank','Prime','Calculation', 'Mersenne Prime')
while count < 8
if isprime(n)
prime_nums = [prime_nums,n] ;
M_n = 2^n - 1;
if isprime(M_n)
mersenne_primes = [mersenne_primes, M_n];
count = count + 1;
fprintf('%5d %5d 2^%d - 1 %15d\n', count, n,n, M_n)
end
end
n = n + 1;
end
end