## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

prime factorization problem ? Posted by tolga_ozsahin on 23 Sept 2005 at 11:38 AM
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
Re: prime factorization problem ? Posted by zibadian on 23 Sept 2005 at 11:56 AM
: 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:
```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;
```

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.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic