prime factorization problem ? - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# prime factorization problem ?

Posts: 4Member
Hello all,

I need help with a program to be written in Turbo Pascal:
"I need to write a program to accept a positive integer and gives its prime factorization, that is, expresses the integer as a product of primes or indicates that it is prime."

Any help would be appreciated.

Thanks,
Tolga

• Posts: 6,349Member
: Hello all,
:
: I need help with a program to be written in Turbo Pascal:
: "I need to write a program to accept a positive integer and gives its prime factorization, that is, expresses the integer as a product of primes or indicates that it is prime."
:
: Any help would be appreciated.
:
: Thanks,
: Tolga
:
The easiest way to find out if a number is prime, is by dividing it by all the odd numbers upto half of that number:
[code]
var
Number: double;
Divisor, MaxDivisor: double;
begin
MaxDivisor := Trunc(Number/2);
Divisor := 3;
if Trunc(Number/2) = Number/2 then { Test if Number is even }
writeln('Number is no prime or is 2')
else
while Divisor < MaxDivisor do
begin
if Trunc(Number/Divisor) = Number/Divisor then
begin
writeln('Number is no prime');
Break;
end;
Divisor := Divisor + 2; {loop through all the odd numbers }
end;
end;
[/code]
The factorization can be done in a similar way. If the loop never indicates that the Number is a prime, then Divisor is one of its factors. All you now need to do is to show that Divisor and its counterpart (Number/Divisor) are primes to complete the prime factorization.