Help writing a for/while loop to identify and sort prime numbers
Show older comments
I have a class assignment that requires using a for or while loop to identify prime numbers between 10-500. After the numbers have been identified as prime, they need to be classified as "twin" if there is another prime within 2, or "isolated" if there is not another prime within 2. The built-in function "isprime" is not allowed.
I have hit a wall and don't even know where to go from here. So far, I have been able to identify prime numbers, but am stuck on the sorting process.
n = 500; %maximum prime number to find
primes = 2;
for i = 1:n %code I found to help identify primes
if mod(i+2,primes) ~= 0
primes = [primes, i+2];
end
3 Comments
Pierre Lonfat
on 26 Apr 2018
Edited: Pierre Lonfat
on 26 Apr 2018
Maybe this is what you are looking for! Give it a quick run and look at result :)
close all
clear all
clc
%===============================
%set your variables:
numbers=[10:500]; %objective of the assignment
within_left=2; %nbr of observerations looking from the left or right
within_right=2; %ex. with 11: (9 10 11(prime) 12 13)
%===============================
% COMPUTATIONS:
%the first existing prime number
primes = 2;
list=[min(numbers)-within_left:max(numbers)+within_right];
for i = 1:max(list)
if mod(i+2,primes) ~= 0
primes = [primes, i+2];
end
end
for i = 1:length(primes)
bounds(1,i)=primes(1,i)-within_left;
bounds(2,i)=primes(1,i)+within_right;
end
% primes numbers and boundaries:
table=[primes(:,min(find(primes>=min(numbers))):min(find(primes>=max(numbers)))-1);...
bounds(:,min(find(primes>=min(numbers))):min(find(primes>=max(numbers)))-1)];
for i = 1:size(table,2)
if max(table(2,i)==primes) || max(table(3,i)==primes) == 1
result{1,i}=table(1,i);
result{2,i}='twin';
else result{1,i}=table(1,i);
result{2,i}='isolated';
end
end
nassnnf
on 7 Dec 2020
can you help me to solve similiar question.the questions is list 50 prime number after 230 in 10x5 matrik
Rik
on 7 Dec 2020
It is your homework. What have you tried? Also, this question has two parts: finding 50 prime numbers and storing them in a 10x5 matrix. Which of these two is causing you trouble?
Answers (1)
Rik
on 26 Apr 2018
You already have a list of all the primes, so you don't need isprime anymore. What you now want to do is figure out the distance between every prime and it's two neighbor primes. You can do this several ways. Because this is a homework question, I will not give a complete working solution.
n = 500; %maximum prime number to find
primes = 2;
for i = 1:n %code I found to help identify primes
if mod(i+2,primes) ~= 0
primes = [primes, i+2];
end
end
inter_prime_distance=diff(primes);
Now you have the distance between each prime pair, you can compare that to 2. You should note that inter_prime_distance is 1 shorter than primes.
If you have more question or need clarification, don't hesitate to comment below.
1 Comment
Rik
on 30 Apr 2018
Did my answer solve your problem? If so, please mark it as accepted answer, if not, feel free to comment with your remaining questions.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!