Thursday, January 7, 2010

Factors and Factorials

All
of you are familiar with factorial of a number. The factorial of a
number N is defined as the product of all integers from 1 to N.
Mathematically it is defined as 1! = 1 and N! = N*(N-1)! But there is a
problem with this, if go for higher values of N, factorials grow very
rapidly as 6! = 720, 11! = 39916800. We can specify such large numbers
with the help of their prime factors. For example: 100 can be written
as (2 2 5 2), means there is 2 two and 2 five is present in 100.


Again
we are left with another problem. To find the prime factors of such
large numbers is very tedious task. So I want help from you people to
solve this problem. Write a code that takes an input any positive
integer N and gives output of N!’s prime factor representation.


 


Input 


Input
will consist of a series of lines, each line containing a single
integer N (2<=N<1000). The file will be terminated by a line
consisting of a single 0.


 


Output 


Output will consist of prime factor representation of N! There will be a blank line between two consecutive outputs.


 


Sample Input 


5                                                                                                                                    


7                                                                                                                                    


0


 


Sample Output 


2 3


3 1


5 1


 


2 4


3 2


5 1


7 1





















































































































































































#include<stdio.h>
int main()
{
                int
num,k=0,i=1,pri[200],j,count=0,tru=0,ans[200][3],m,n,inp,diff=0;
                //ans[][]array
stores the different prime factors and the numbers of times each occur in the
prime factorization of the factorial of the input integer.
                //clrscr();
                printf("Enter the number:
");
                scanf("%d",&inp);
                //Generating the first 100 prime
numbers and storing them in the pri[] array.
                while(count<200)
                {
                                tru=0;
                                i++;
                                for(j=2;j<=i/2;j++)
                                {
                                                if(i%j==0)
                                                {
                                                                tru=1;
                                                                break;
                                                }
                                }
                                if(tru!=1)
                                {
                                                pri[count]=i;
                                                count++;
                                }
                }
                /*printf("The prime numbers
are:\n");
                for(i=0;i<count;i++)
                {
                                printf("%d,",pri[i]);
                }*/
                n=2;
                while(n<=inp)
                {
                                 i=0;tru=0;
                                num=n;
                                 //Doing the prime factorization.
                                while(num>1)
                                {
                                                 j=0;tru=0;
                                                while(num%pri[i]==0)
                                                {
                                                                 m=0;
                                                                tru=1;
                                                                num=num/pri[i];
                                                                j++;
                                                }
                                                if(tru==1)
                                                {
                                                                if(diff==0)
                                                                 {
                            
                                                 ans[0][0]=pri[i];
                            
                                                 diff++;
                            
                                                 ans[0][1]=j;
                            
                                   }
                                                                else
                                                                {
                                                                                while(pri[i]!=ans[m][0]
&& m<diff)
                                                                                {
                                                                                              m++;
                                                                                }
                                                                                if(m==diff)
                                                                                {
                                                                                                ans[m][0]=pri[i];
                                                                                                ans[m][1]=j;
                                                                                                diff++;
                                                                                }
                                                                                else
                                                                                {
                         
                                                                      ans[m][1]=ans[m][1]+j;
                         
                                                      }
                         
                                      }
                                                }
                                                i++;
                                }
                                n++;
                }
                printf("The prime factorization
is:\n");
                //Printing the prime factorization in
the correct format.
                for(m=0;m<diff;m++)
                {
                                printf("%d
%d\n",ans[m][0],ans[m][1]);
                }
                //printf("\n");
                //getch();
                //scanf("%d",&i);
                return 0;
}



Enter the number: 5
The prime factorization is:
2 3
3 1
5 1

Enter the number: 7
The prime factorization is:
2 4
3 2
5 1
7 1

No comments:

Post a Comment