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

prime factorization problem ?

tolga_ozsahintolga_ozsahin 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

Comments

  • zibadianzibadian 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.
Sign In or Register to comment.