Prime Factorial

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)
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.