-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathprime_factorial.m
51 lines (46 loc) · 1.41 KB
/
prime_factorial.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
clear all
clc
%% Prime Factors
% This code gets user input number, calculates and displays its prime factors.
% For this, first it determines prime numbers which are less than or equal to
% user input number. Then if the input is dividable by that prime number,
% it becomes one of input's prime factors.
%% Request user input
prompt = 'Input your number: ';
n = input(prompt);
%%
counter = 0; % initialize number of prime factors
if n <= 1
disp('input must be positive integer greater than 1')
else if floor(n)~= n
disp('input must be positive integer')
else
for i = 2:1:n
if i == 2
isprime = 1;
else
half_i = floor(i/2)+1;
j = 2;
while j <= half_i %lines 16 to 30 check if i is prime or not.
residual = mod(i,j);
if residual == 0
isprime = 0;
break
else if j == half_i
isprime = 1;
break
else
j=j+1;
end
end
end
end
if isprime == 1 && mod(n,i) == 0
counter=counter+1;
f(counter) = i; % prime factors of n will be storing
end
end
end
end
disp('Prime factors of input number are: ')
disp(f)