Extra safe Prime Number

A safe prime is a prime number of the form (2 * p) + 1 where p is also a prime.

5 = 2 * 2 + 1

7 = 2 * 3 + 1

11 = 2 * 5 + 1

You can check more about safe prime number in the below link:


MATLAB CODE to check a number is safe prime or not:


clc
clear all
close all
x=input('Enter the number');
y=[];
qam=0;
for re=2:(x-1)
    if(rem(x,re)==0)
        qam=qam+1;
    end
end
if(qam==0)
for i=2:(x-1)
    m=i;
    c=0;
    for j=2:m-1
        if(rem(m,j)==0)
            c=c+1;
        end
    end
    if(c==0)
        y=[y i];
    end
end
d=0;
for i=1:length(y)
    if(2*y(i)+1==x)
        d=d+1;
    end
end
if(d==1)
    disp("Yes");
else
    disp("No");
end
else
    disp('Not a prime number');
end

Explanation:




Sample Output:



MATLAB CODE to print all safe prime numbers less than or equal to the input number:

clc
clear all
close all
uo=[];
g=input('Enter the number');
for x=1:g
y=[];
qam=0;
for re=2:(x-1)
    if(rem(x,re)==0)
        qam=qam+1;
    end
end
if(qam==0)
for i=2:(x-1)
    m=i;
    c=0;
    for j=2:m-1
        if(rem(m,j)==0)
            c=c+1;
        end
    end
    if(c==0)
        y=[y i];
    end
end
d=0;
for i=1:length(y)
    if(2*y(i)+1==x)
        d=d+1;
    end
end
if(d==1)
    uo=[uo x];
end
end
end

You can check the safe prime numbers list in the below link:


Sample Output:




    
MATLAB CODE to check an input prime number is extra safe or not:

clc
clear all
close all
x=input('Enter the number');
y=[];
qam=0;
for re=2:(x-1)
    if(rem(x,re)==0)
        qam=qam+1;
    end
end
if(qam==0)
for i=2:(x-1)
    m=i;
    c=0;
    for j=2:m-1
        if(rem(m,j)==0)
            c=c+1;
        end
    end
    if(c==0)
        y=[y i];
    end
end
d=0;
for i=1:length(y)
    if(2*y(i)+1==x)
        d=y(i);
    end
end
x=d;
y=[];
qam=0;
for re=2:(x-1)
    if(rem(x,re)==0)
        qam=qam+1;
    end
end
if(qam==0)
for i=2:(x-1)
    m=i;
    c=0;
    for j=2:m-1
        if(rem(m,j)==0)
            c=c+1;
        end
    end
    if(c==0)
        y=[y i];
    end
end
d=0;
for i=1:length(y)
    if(2*y(i)+1==x)
        d=d+1;
    end
end
if(d==1)
    disp('true');
else
    disp('false');
end
else
    disp('false');
end
else
    disp('false');
end

Sample Output:



(because 23=2*11+1 and 11 is also a safe prime (11=2*5+1))

Function implementation:



function tf = isextrasafe(x)
y=[];
qam=0;
for re=2:(x-1)
if(rem(x,re)==0)
qam=qam+1;
end
end
if(qam==0)
for i=2:(x-1)
m=i;
c=0;
for j=2:m-1
if(rem(m,j)==0)
c=c+1;
end
end
if(c==0)
y=[y i];
end
end
d=0;
for i=1:length(y)
if(2*y(i)+1==x)
d=y(i);
end
end
x=d;
y=[];
qam=0;
for re=2:(x-1)
if(rem(x,re)==0)
qam=qam+1;
end
end
if(qam==0)
for i=2:(x-1)
m=i;
c=0;
for j=2:m-1
if(rem(m,j)==0)
c=c+1;
end
end
if(c==0)
y=[y i];
end
end
d=0;
for i=1:length(y)
if(2*y(i)+1==x)
d=d+1;
end
end
if(d==1)
tf=true;
else
tf=false;
end
else
tf=true;
end
else
tf=false;
end
end

No comments