Question

VIEIJ. Put 444 KB) Page 1 of 3 20. A safe prime is a prime number...

VIEIJ. Put 444 KB) Page 1 of 3 20. A safe prime is a prime number that can be written in the form 2p +1 wherep is also a prime number. For example, 47 is a safe prime since 47 =2x23 +1 and 23 is also a prime number. Write a computer program that finds and displays all the safe primes between 1 and 1,000. Do not use MATLABs built-in function isprime. A BU DO WAY Ox) v Pg Me O EDIT

Homework Answers

Answer #1

%matlab code
function is_prime = checkPrime(n)
if (n <= 1)
is_prime = false;
return
elseif (n <= 3)
is_prime = true;
return
end
  
if (mod(n,2) == 0 || mod(n,3) == 0)
is_prime = false;
return
end

i = 5;
while i*i <= n
if (mod(n,i) == 0 || mod(n,(i+2)) == 0)
is_prime = false;
return
end
i = i+6;
end
is_prime = true;
end


% find safe prime
a = 1;
b = 1000;
for i = a:b
if (checkPrime(i) && (mod(i, 2) == 1) && checkPrime((i-1)/2))
fprintf("%d ", i)
end
end
fprintf("\n")


Sample output:

5 7 11 23 47 59 83 107 167 179 227 263 347 359 383 467 479 503 563 587 719 839 863 887 983

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT