-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prime_Plot.m
61 lines (57 loc) · 1009 Bytes
/
Prime_Plot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function result=prime(num)
count = 0;
for i=1:num
if mod(num,i)==0
count=count+1;
endif
endfor
if count==2
result=1;
else
result=0;
endif
endfunction
function average=percent(limit)
count=0;
i=2;
sum=0;
while(i>=2)
if prime(i)==1
if i<limit
sum=sum+i;
count=count+1;
endif
endif
i=i+1;
if i>limit
break;
endif
endwhile
average=(count/limit)*100
endfunction
function avg=twinpercent(lmt)
cnt = 0;
i=2;
while(i>=2)
if prime(i)==1
if prime(i+2)==1
cnt = cnt+2;
endif
endif
i=i+1;
if i>=lmt
break;
endif
endwhile
avg=(cnt/lmt)*100
endfunction
number = 1:10:1000
plot(number,arrayfun(@percent, number),'-xr')
hold on;
xlabel('Numbers')
ylabel('Percentage')
plot(number,arrayfun(@twinpercent, number),'-ob')
hold on;
plot(number,log(number),'-og')
hold off;
legend('Prime','Twinprimes','logarithm')