how to find prime numbers by using mod function

36 次查看(过去 30 天)
I'm wondering Is it possible to find prime numbers by using mod() function?. If yes, please I need your some basic help. for example how i could find prime number from 2:100. (( without using prime function ))
  2 个评论
Stephen23
Stephen23 2016-9-14
The accepted answer is very slow. See José-Luis' answer for a much simpler and more efficient solution.

请先登录,再进行评论。

采纳的回答

José-Luis
José-Luis 2016-9-14
编辑:José-Luis 2016-9-14
Much faster than @JohnBG
tic
primes_list=[];
for k=1:50000
L=mod(k,[1:50000]);
L2=find(L==0);
if length(L2)<3
primes_list=[primes_list k];
end
end
toc
tic
primes = 2;
for ii = 2:50000
if all(mod(ii,primes))
primes = [primes, ii];
end
end
toc
Elapsed time is 14.439689 seconds.
Elapsed time is 0.963569 seconds.
Note that both algorithms can easily be made almost twice as fast by taking a step of two instead of one in the loop. There are certainly smarter ways of doing this anyway.

更多回答(2 个)

Walter Roberson
Walter Roberson 2016-9-13
For example,
mod(4:100, 3)
is 0 at all multiplies of 3, so where mod(4:100, 3) is 0, the value cannot be prime.
(Notice this applies when the initial value in the vector is greater than the modulus)
  2 个评论
John BG
John BG 2016-9-13
Walter
Mohame is asking for a list of primes, not a sawtooth signal
Walter Roberson
Walter Roberson 2016-9-14
John:
I explained the key to understanding how mod can be used to filter multiples. This is, after all, obviously a homework question, and providing complete answers to homework questions is not good practice.

请先登录,再进行评论。


John BG
John BG 2016-9-13
Mohame
primes_list=[]
for k=1:1:200
L=mod(k,[1:200]);
L2=find(L==0);
if length(L2)<3
primes_list=[primes_list k];
end
end
the list is here
primes_list
=
Columns 1 through 5
1.00 2.00 3.00 5.00 7.00
Columns 6 through 10
11.00 13.00 17.00 19.00 23.00
Columns 11 through 15
29.00 31.00 37.00 41.00 43.00
Columns 16 through 20
47.00 53.00 59.00 61.00 67.00
Columns 21 through 25
71.00 73.00 79.00 83.00 89.00
Columns 26 through 30
97.00 1.00 2.00 3.00 5.00
Columns 31 through 35
7.00 11.00 13.00 17.00 19.00
Columns 36 through 40
23.00 29.00 31.00 37.00 41.00
Columns 41 through 45
43.00 47.00 53.00 59.00 61.00
Columns 46 through 50
67.00 71.00 73.00 79.00 83.00
Columns 51 through 55
89.00 97.00 101.00 103.00 107.00
Columns 56 through 60
109.00 113.00 127.00 131.00 137.00
Columns 61 through 65
139.00 149.00 151.00 157.00 163.00
Columns 66 through 70
167.00 173.00 179.00 181.00 191.00
Columns 71 through 73
193.00 197.00 199.00
Mohame
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  7 个评论
Walter Roberson
Walter Roberson 2016-9-25
John BG:
"The Primes Page: prime number research, records, and resources" (The University of Tennessee at Martin)
"Why is the number one not prime?
[...]Answer One: By definition of prime!
[...]Answer Two: Because of the purpose of primes
[...]Answer Three: Because one is a unit.
[...]Answer Four: By the Generalized Definition of Prime.
[...]"
It goes right back to Euclid's formal definition of primes.
You are, of course, welcome to submit your own paper to the Journal of Integer Sequences to prove that Euclid was wrong.
George Kara
George Kara 2018-9-10
编辑:George Kara 2018-9-10
No need to stress out guys you can just use this:
function [Primes_list]=MyPrime(n)
But still your answer is false because i get numbers like 1 4 9 etc.
if n>=2
Primes_list=[]
for k=2:1:n
L=mod(k,[2:n]);
L2=find(L==0);
if length(L2)<3
Primes_list=[Primes_list k];
end
end
else
disp('n must me bigger that "2"')
end
end
"n" is the number you want it to search to if n == 100 then it will search from [2,3,4.....98,99,100]

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by