Write a function prime_isolated(a, b) that returns as output (argument) ALL prim
ID: 3810588 • Letter: W
Question
Write a function prime_isolated(a, b) that returns as output (argument) ALL prime numbers p in the interval [a, b] (inclusive) for which neither p - 2 NOR p + 2 arc prime numbers (which are called isolated primes). The "Sieve of Eratosthenes" (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) is the simplest way to find prime numbers, but you can choose a different algorithm. (Do NOT use the built-in MATLAB function isprime other than to check your results.) When given only one input, your function should find all isolated primes in the interval [1, a]. You must VERIFY that the numbers your function found arc isolated primes (e.g. by comparing it to lists of isolated prime numbers or using the built-in MATLAB function isprime). Write a detailed structure plan implementing your algorithm that satisfies the program scope- specification laid out above. Write the function primeisolated based on YOUR structure plan. Use your function to do the following: find all isolated primes below 20 by calling prime_isolated(20) find all isolated primes in the interval [20, 50] by calling prime_isolated(20, 50) find all isolated primes in the interval [50, 100] by calling prime_isolated(50, 100)Explanation / Answer
prime1=prime_isolated(20);
disp('prime numbers under 20')
prime1
prime2=prime_isolated2(20,50);
disp('prime numbers from 20 to 50')
prime2
prime3=prime_isolated2(50,100);
disp('prime numbers from 50 to 100')
prime3
function prime = prime_isolated (b)
a=1;
if (a>b || a <0 || b < 0)
error('ERROR: Invalid Input');
end
prime = [];
for j=0:(b-a)
if all(mod((a+j),2:((a+j)/2))), prime = [prime;a+j];
end
end
function prime = prime_isolated2(a,b)
if (a>b || a <0 || b < 0)
error('ERROR: Invalid Input');
end
prime = [];
for j=0:(b-a)
if all(mod((a+j),2:((a+j)/2))), prime = [prime;a+j];
end
end
output
prime numbers under 20
prime1 =
1
2
3
5
7
11
13
17
19
prime numbers from 20 to 50
prime2 =
23
29
31
37
41
43
47
prime numbers from 50 to 100
prime3 =
53
59
61
67
71
73
79
83
89
97
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.