-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I changed the prime_factorial.m file into a function with a more adva…
…nced algorithm in order to calculate the factorization of prime numbers. The script is easy to read and to understand for the user
- Loading branch information
Showing
1 changed file
with
55 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |