Pascal

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

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
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
Report
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
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.