Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized and reworded the algorithm #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 55 additions & 48 deletions algorithms/maths/prime_factorial.m
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)