prime factorial Algorithm

The prime factorial algorithm is a unique mathematical method used for representing numbers by the product of their prime factors. This algorithm is based on the fundamental theorem of arithmetic, which states that every integer greater than 1 can be uniquely expressed as the product of prime numbers. This theorem has significant implications in number theory, cryptography, and computational efficiency. In the prime factorial algorithm, a given number is systematically broken down into its prime factors by dividing it by the smallest prime number possible, starting with 2, and continuing until the quotient is a prime number itself. This process generates a sequence of prime factors, which, when multiplied together, yield the original number. For example, applying the algorithm to the number 12 would yield the prime factors 2, 2, and 3 (since 12 = 2 x 2 x 3). The prime factorial representation not only provides insight into the underlying structure of numbers but also plays a crucial role in several computational tasks, such as finding the greatest common divisor (GCD) and least common multiple (LCM) of numbers, and solving problems related to cryptography and computer algorithms.
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)

LANGUAGE:

DARK MODE: