How can I write a program that can display the first 50 consecutive prime numbers starting with 2?

조회 수: 8 (최근 30일)
Primes(x) and isPrime(x) are not allowed for this question. fprintf should be used. I was thinking of using a while loop with maybe a for and/or if's inside. I am new to MATLAB so any help would be appreciated. Thanks!
  댓글 수: 3
John D'Errico
John D'Errico 2021년 4월 29일
There are only two prime numbers that are consecutive, that is, 2 and 3. All other primes are NOT consecutive. So the sum of all consecutive primes is 5. It can be no other value. ;-)

댓글을 달려면 로그인하십시오.

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 3월 15일
for ii = 1:inf
nguess = ii;
mat = tril(bsxfun(@times,2:nguess,(2:nguess).'));
pn = setdiff(2:nguess,mat(logical(mat)));
if numel(pn)>50
pn = pn(1:50);
break
end
end
Maybe?
  댓글 수: 4
Sean de Wolski
Sean de Wolski 2012년 3월 15일
How about really simple?:
disp([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229])
Walter Roberson
Walter Roberson 2012년 3월 15일
The assignment requires that fprintf() be used, so disp() would not be acceptable.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

G A
G A 2012년 3월 15일
if a is a prime then length(factor(a)) is equal to 1. You can try while loop.

Walter Roberson
Walter Roberson 2012년 3월 15일
If you know a list of primes P, then multiply them all together and add 1. The resulting number will either be prime itself, or will be a composite number that can be factored into at least two primes that are not already on your list. Each time you generate one of these new smaller primes and insert it on to your list, you can take the subset of the list up to that point and start generating from there.

Bud Kelly
Bud Kelly 2018년 3월 31일
편집: Walter Roberson 2018년 3월 31일
Here's a small and very tight bit of code that will generate prime numbers up to any searchlimit you wish. It's fast too. On my Macbook Pro it lists primes up to 10,000 in only 0.112 seconds. Fast enough? Here's the code:
searchlimit = 100; % here you can set any searchlimit you wish
primes = 2; % initialize list starting with 2
for i = 1 : searchlimit % begin for loop
if mod(2+i, primes) ~= 0 % go through i's by 2
primes = [primes; 2+i]; % if prime add to primes list
end
end % end for loop
primes % print list of primes in single column (note: no semicolon!)
toc; % end timer and display elapsed time in seconds
% for your reference, the prime numbers less than 100 are:
% 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
% 59, 61, 67, 71, 73, 79, 83, 89, 97
  댓글 수: 1
John D'Errico
John D'Errico 2018년 3월 31일
I would not call it tight. Dynamic growth of an array, which is a huge problem here. See how incredibly slowly this code runs to find all primes less than 1e6. (6 minutes or so on my old mac. It even forced my CPU fan to kick on.) This code checks all the even numbers greater than 2. It performs far more modulus tests than is necessary.
Just use a sieve. Way faster. A simple implementation of a basic sieve required 0.080629 seconds on my old Mac to generate all 78498 primes less than 1e6.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by