Matlab Finding Prime numbers w/o built in functions...

Thread Starter

Judas543

Joined Jan 26, 2010
60
1) Write a program (an m-file) to compute the number of prime numbers for every “century” (i.e., # primes in the range [1-100], in [101-200], in [201-300], etc.) up to 100000. Plot your results. Approximately what percentage of numbers seems to be prime? Is this a good figure for all centuries?


Normally I could use primes, isprime or factor functions but my professor wants us to do it with loops, and conditional statements...


What I'm thinking that this could be a nested for loop? Could someone help me form this script please.What formula would i need to find the number of prime numbers for every century
 

johndoe45

Joined Jan 30, 2010
364
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.
don't have the time to help you out.
but it is the exact same as the dice one. just use


r=1:100:99901;
s=100:100:100000;

for i=1:length(r)
t=r(i):s(i);
end

%therefore going to be 1:100 , 101:200, 201:301 ...........99901:100000

if z==2 || z==3 || z==5 || z==7 || z==11 || z==13 || z==17 || z==19 || z==23 || z==29 || z==31 || z==37 || z==41 || z==43 || z==47 || z==53 || z==59 || z==61 || z==67 || z==71 || z==73 || z==79 || z==83 || z==89 || z==97

don't even think need a for loop
unless want to use n!+-1
for ex. 1!+1=2 , 2!+1=3, 2!-1=1, 3!+1=7 , 3!-1,=5
just use first method.

just if statements.

and the count functions like in dice game
 
Last edited:

johndoe45

Joined Jan 30, 2010
364
NOT COMPLETE. "IF" STATEMENTS GOING TO HAVE TO DO SOME RESEARCH. CAN'T FIGURE OUT A WAY TO PUT PRIME AS A FUNCTION IN TERMS OF n-1 OR SOMETHING WITH n. MAYBE ITS IMPOSSIBLE!!!!!!!!!! BUT CAN ALWAYS USE NUMERICAL METHOD APPROACHES

Algorithm

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself.





To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
  1. Create a list of consecutive integers from two to n: (2, 3, 4, ..., n),
  2. Initially, let p equal 2, the first prime number,
  3. While enumerating all multiples of p starting from p2, strike them off from the original list,
  4. Find the first number remaining on the list after p (it's the next prime); let p equal this number,
  5. Repeat steps 3 and 4 until p2 is greater than n.
  6. All the remaining numbers in the list are prime.
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
clc
clear all
close all

gap=100; %gap between years
q=1; %first year
w=100000; %last year

r=q:gap:w-gap+1;
s=q+gap-1:gap:w;
k=1:gap;

for i=1:length(r)
for j=1:gap
t(i,k)=r(i):s(i); %this is vector 1-100, 101-200, 201-300, 301-400.... 99901-100000 "centuries"
count_a(i,j)=0; %counting variable
if t(i,j) == 2 || t(i,j) == 5
count_a(i,j)=count_a(i,j)+1; %counts primes in vector form. to check a single century
%type count_a(whatever century you want,k)
end %if want to see sum of single century type count_a(whatever century,j)
end
end

v=sum(count_a')
%this is the vector as a 1 row 1000 columns summing all prime numbers in a century. of course
%coulumn one (1-100 aka 1st century) is going to have 2 prime numbers and everything else zero
%cause of the "if" statement being only 2 and 5 if want to see how anything works just take off
%the ; at end of a function
plot(1:length(r),v)
xlabel('centuries (years)')
ylabel('prime numbers')
title('Prime Numbers in a Century')
 
Last edited:

Thread Starter

Judas543

Joined Jan 26, 2010
60
This what I found which makes sense, but I can't figure out a way to do it by every 100 years? Unless it already does it


clear
clc

n = 25;
idx = true(1,fix(n/2));

for ii = 3:2:sqrt(n)
if idx((ii+1)/2)
idx(((ii^2 + 1)/2):ii:end) = false;
end
end

isprimesValue = 1:2:n
isprimes = isprimesValue(idx);
isprimes(1) = 2
 

johndoe45

Joined Jan 30, 2010
364
here is method to count primes
has nothing to do with the centuries though
can shorten it with more loops
your professor gave you a hard task unless there is an easy way that i'm not seeing.
this should be like a final exam :D
Should be happy with just this.

THIS GENERATES AND COUNTS ALL PRIME NUMBERS FROM 1-168. NO MORE THAN THAT OR IT IS INCORRECT.


clc
clear
all
close all


u=168;

t=1:1:u;
z=1:1:u;
x=1:1:u;
c=1:1:u;
s=1:1:u;
a=1:1:u;

q=t*2;
w=t*3;
e=t*5;
r=t*7;
aa=t*9;
p=t*11;

t(q)=0;
z(w)=0;
x(e)=0;
c(r)=0;
s(aa)=0;
a(p)=0;

j=t(1:q-1:u);
b=z(1:w-2:u);
n=x(1:e-4:u);
m=c(1:r-6:u);
d=s(1:aa-8:u);
v=a(1: p-10:u);


for i=1:u

if i<4
j(i)=1;
end

if
i <9
b(i)=1;
end

if
i<25
n(i)=1;
end

if
i < 49
m(i)=1;
end

if
i < 81
d(i)=1;
end

if
i < 121
v(i)=1;
end

end

k=v.*d.*m.*n.*b.*j;
g=1:u;
f=g.*k;

for e=1:u
if f(e)>0
f(e)=1;
end
end

o=f.*g;
count=0;

for y=1:u
if o(y) == 1
o(y)=0;
end
if
o(y) > 0
count=count+1;
end
end

o
count
 
Last edited:

John_2016

Joined Nov 23, 2016
55
clear all;clc;close all

N=5e3
L=1:N;
tic
for k=1:1:numel(L)
L2{k}=[1:L(k)];
end

prime_list=[]
for k=1:1:N
if nnz(mod(k*ones(1,numel(L2{k})),L2{k}))==k-2
prime_list=[prime_list k];
end
end
toc
Elapsed time is 0.881085 seconds.

% save prime_list.mat % file size 16MB >10MB upload limit
% save prime_list.txt prime_list % when opening text file not for humans


prime_list_char=num2str(prime_list)
file_id=fopen('prime_list.txt','w');
fprintf(file_id,'Primes below %s :\n\n',num2str(N));
fprintf(file_id,'%s',prime_list_char);
fclose(file_id)
 
Last edited:
Top