Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decomposition of the factors
11-09-2009, 05:38 AM
Post: #1
Decomposition of the factors
This is an interesting problem. The point is to extract factors of the number. When you multiplicate those factors, you need to get entered number "n".
Input
From standard input, read integer "n".
Output
Output in the next lines all prime factors.
Sample Input
493
Sample Output
17
29
Sample Input
12
Sample Output
2
2
3
So basically, you don't need to check for prime numbers, because if the number is divideable with 2 it will be the first number that is going to be written . . .
I made fast and small solution, first TPW :
Code:
program factors;
uses wincrt;
var
n,i:longint;
begin
writeln('Enter n !');
readln(n);
i:=2;
while (n>1) do
begin
   if n mod i = 0 then
     begin
       n:=n div i;
       writeln(i);
       i:=1;
     end;
     i:=i+1;
end;
end.
And then C++ :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

int main()
{
    int i,n;
    cout<<"Enter n !"<<endl;
    cin>>n;
    i=2;
    while (n > 1)
    {
        if (n % i == 0)
        {
          n=n/i;
          cout<<i<<endl;
          i=1;
         }
         i++;  
    }
    system("pause");
}

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: