Translate

Friday 23 January 2015

FACTCG2 solution(Medium Factorization)

LOGIC::
here u have to use sieve approach but instead of find prime number upto
10^7 just find out that the smallest number greater than1 which divide that number.
U can easily find using sieve approach>>>>>>>>>>>

HERE MY AC C++ solution is::::
if u have any doubt in my logic plz ask me in comment..........
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#define max 10000004
int a[max]={0};
void sieve(){
        for(int i=2;i<=max;i++){
                if(a[i]!=0)continue;
                for(int j=2*i;j<=max;j+=i){
                        if(a[j]==0)
                        a[j]=i;
                }        
        }        
}

int main(){
    int n;
    sieve();
    while(scanf("%d",&n)!=EOF){                               
                               if(n==1){printf("%d\n",n);continue;}
                               else printf("1");
                               if(a[n]!=0)
                               while(n%a[n]==0){printf(" x %d",a[n]);if(a[n]==0)break;n/=a[n];if(a[n]==0)break;}         
                               printf(" x %d",n);
                               printf("\n");                         
    }
    return 0;    
}

1 comment:

মিষ্টু said...

How can you declare the a[max]. The size of array is overflow.
And your code is compilation error in ideone online compiler

Working With Java Collections