Skip to content

Commit e95ae05

Browse files
committed
Optimized and reworded the algorithm
The new algorithm is more efficient and much easier for the user to read and to understand. This is now a useful function in order to calculate the factorization of prime numbers, with some errors that will show up if the number introduced by the user is not an integer higher than 1.
1 parent e150b77 commit e95ae05

File tree

1 file changed

+55
-48
lines changed

1 file changed

+55
-48
lines changed

algorithms/maths/prime_factorial.m

+55-48
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
1-
clear all
2-
clc
3-
4-
%% Prime Factors
5-
% This code gets user input number, calculates and displays its prime factors.
6-
% For this, first it determines prime numbers which are less than or equal to
7-
% user input number. Then if the input is dividable by that prime number,
8-
% it becomes one of input's prime factors.
9-
10-
%% Request user input
11-
prompt = 'Input your number: ';
12-
n = input(prompt);
13-
14-
%%
15-
counter = 0; % initialize number of prime factors
16-
17-
if n <= 1
18-
disp('input must be positive integer greater than 1')
19-
else if floor(n)~= n
20-
disp('input must be positive integer')
21-
else
22-
for i = 2:1:n
23-
if i == 2
24-
isprime = 1;
25-
else
26-
half_i = floor(i/2)+1;
27-
j = 2;
28-
while j <= half_i %lines 16 to 30 check if i is prime or not.
29-
residual = mod(i,j);
30-
if residual == 0
31-
isprime = 0;
32-
break
33-
else if j == half_i
34-
isprime = 1;
35-
break
36-
else
37-
j=j+1;
38-
end
39-
end
40-
end
41-
end
42-
if isprime == 1 && mod(n,i) == 0
43-
counter=counter+1;
44-
f(counter) = i; % prime factors of n will be storing
45-
end
46-
end
1+
%% Prime Factorization
2+
3+
function prime_factorization()
4+
5+
% This function gets user input number, calculates and displays its prime factors.
6+
%
7+
% 1) Input: The code asks the user to enter a number to decompose into prime factors.
8+
%
9+
% 2) Input Validation: It checks if the number is a positive integer greater than 1.
10+
% If not, it throws an error.
11+
%
12+
% 3) Prime Factorization:
13+
% Firstly, it divides the number by 2 repeatedly (if it's divisible by 2).
14+
% Then, it checks for odd divisors (starting from 3) up to the square root
15+
% of the remaining number, dividing when it finds a factor.
16+
%
17+
% 4) Output: The prime factors are stored in an array and displayed in the
18+
% format n = p1 * p2 * ...
19+
20+
% Ask the user to enter a number
21+
number = input('Enter a number to decompose into prime factors: ');
22+
23+
% Check if the number is a positive integer greater than 1
24+
if mod(number, 1) ~= 0 || number <= 1
25+
error('The number must be a positive integer greater than 1.');
26+
end
27+
28+
% Initialize an empty array to store the prime factors
29+
primeFactors = [];
30+
31+
% Check for factor 2 (the smallest prime)
32+
while mod(number, 2) == 0
33+
primeFactors = [primeFactors, 2];
34+
number = number / 2;
35+
end
36+
37+
% Check for odd factors starting from 3
38+
divisor = 3;
39+
while divisor * divisor <= number
40+
while mod(number, divisor) == 0
41+
primeFactors = [primeFactors, divisor];
42+
number = number / divisor;
4743
end
44+
divisor = divisor + 2; % Skip even numbers as they are not primes
45+
end
46+
47+
% If the remaining number is greater than 2, it must be prime
48+
if number > 2
49+
primeFactors = [primeFactors, number];
50+
end
51+
52+
% Display the prime factorization
53+
disp('The prime factorization is:');
54+
fprintf('%d = ', prod(primeFactors)); % Print the original number
55+
disp(strjoin(arrayfun(@num2str, primeFactors, 'UniformOutput', false), ' * '));
56+
4857
end
4958

50-
disp('Prime factors of input number are: ')
51-
disp(f)

0 commit comments

Comments
 (0)