From e95ae050027a6085c7128a7b9c9cd0335dcf97ce Mon Sep 17 00:00:00 2001 From: Jorge Date: Fri, 13 Dec 2024 18:23:01 +0100 Subject: [PATCH] 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. --- algorithms/maths/prime_factorial.m | 103 +++++++++++++++-------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/algorithms/maths/prime_factorial.m b/algorithms/maths/prime_factorial.m index 94510ee..991baad 100644 --- a/algorithms/maths/prime_factorial.m +++ b/algorithms/maths/prime_factorial.m @@ -1,51 +1,58 @@ -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 +%% Prime Factorization + +function prime_factorization() + +% This function gets user input number, calculates and displays its prime factors. +% +% 1) Input: The code asks the user to enter a number to decompose into prime factors. +% +% 2) Input Validation: It checks if the number is a positive integer greater than 1. +% If not, it throws an error. +% +% 3) Prime Factorization: +% Firstly, it divides the number by 2 repeatedly (if it's divisible by 2). +% Then, it checks for odd divisors (starting from 3) up to the square root +% of the remaining number, dividing when it finds a factor. +% +% 4) Output: The prime factors are stored in an array and displayed in the +% format n = p1 * p2 * ... + +% Ask the user to enter a number +number = input('Enter a number to decompose into prime factors: '); + +% Check if the number is a positive integer greater than 1 +if mod(number, 1) ~= 0 || number <= 1 + error('The number must be a positive integer greater than 1.'); +end + +% Initialize an empty array to store the prime factors +primeFactors = []; + +% Check for factor 2 (the smallest prime) +while mod(number, 2) == 0 + primeFactors = [primeFactors, 2]; + number = number / 2; +end + +% Check for odd factors starting from 3 +divisor = 3; +while divisor * divisor <= number + while mod(number, divisor) == 0 + primeFactors = [primeFactors, divisor]; + number = number / divisor; end + divisor = divisor + 2; % Skip even numbers as they are not primes +end + +% If the remaining number is greater than 2, it must be prime +if number > 2 + primeFactors = [primeFactors, number]; +end + +% Display the prime factorization +disp('The prime factorization is:'); +fprintf('%d = ', prod(primeFactors)); % Print the original number +disp(strjoin(arrayfun(@num2str, primeFactors, 'UniformOutput', false), ' * ')); + end -disp('Prime factors of input number are: ') -disp(f)