Page 12 of 12

Re: 583 - Prime Factors

Posted: Thu Oct 16, 2014 6:36 pm
by pinou
I get a WA with that code, can anyone tell me why:

Code: Select all

Thank you brianfry713, got an AC 

Re: 583 - Prime Factors

Posted: Thu Oct 16, 2014 7:21 pm
by brianfry713
Try running your code on the sample input.

Re: 583 - Prime Factors

Posted: Thu Oct 16, 2014 8:54 pm
by pinou
I tested the sample input the answers are correct.

Re: 583 - Prime Factors

Posted: Fri Oct 17, 2014 10:26 pm
by brianfry713
Don't print a space at the end of the line.

To read and print a long, use %li or %ld instead of %i.

Re: 583 - Prime Factors

Posted: Sat Aug 22, 2015 4:06 pm
by prokawsar
Getting TLE. Please help me.

Code: Select all

/* Date: 22.08.15
Program: Prime factors

*/
#include <stdio.h>
#include <math.h>

void primefact(long int n);

int main(){

    long int n ;

    h:
        scanf("%ld", &n);
        if(n==0) return 0;
        if(n>0){
            printf("%ld = ", n);
            primefact(n);
            printf("\n");
        }
        else {
            printf("%ld = -1 x ", n);
            n = abs(n);
            primefact(n);
            printf("\n");
        }
    if(n!=0)  goto h;

    return 0;
}

void primefact(long int n){

int i, j, f;

for(i=2; ; i++){
        for(j=2; ; j++){
            f=n%j;
            if(f== 0){
                printf("%d", j);
                n/=j;
                break;
            }
        }
        if(n==1) break;
        printf(" x ");
    }
}

Re: 583 - Prime Factors

Posted: Fri Mar 03, 2017 12:29 pm
by ainan.ahmed
Can anyone help?? getting WA .. :(

Code: Select all

#include <bits/stdc++.h>
using namespace std;
#define mkp             make_pair
#define pii             pair<int,int>
#define PI              acos(-1)
#define sc(a)           scanf("%d",&a)
#define sc2(a,b)        scanf("%d%d",&a,&b)
#define sc3(a,b,c)      scanf("%d%d%d",&a,&b,&c)
#define pr(a)           printf("%d\n",a)
#define PI acos(-1)
#define MX   700000
int primes[MX];
bool flag[MX];
int total=0;
void sieve()
{
    for(int i=4;i<=MX;i+=2)
        flag[i]=1;
    int sq=sqrt(MX)+1;

        for(int i=3;i<=sq;i+=2)
        {
            if(flag[i]==0)
            {
                for(int j=i*i;j<=MX;j+=i)
                {
                    flag[j]=1;
                }



            }
        }
        for(int i=2;i<=MX;i++)
        {
            if(!flag[i]){
                primes[total]=i;
                total++;

            }
        }

}
int main()
{

   //  freopen("input.txt","r",stdin);
  //   freopen("output.txt","w",stdout);
    sieve();
    //cout<<total<<endl;
    int n,temp;
    while(scanf("%d",&n))
    {
        if(n==0)return 0;

        if (n<0)
                printf("%d = -1 x",n);
            else
                 printf("%d =",n);
                 n=abs(n);
        if(n==1)
        {
            printf(" 1\n",n);
            continue;
        }


                 temp=n;
                 long long sum=1;
        for(int i=1;i<total&&primes[i]<=temp;i++)
        {

            if(temp%primes[i]==0)
            {
                while(temp%primes[i]==0)
                {

                    sum*=primes[i];
                    if(sum!=n)
                    printf(" %d x",primes[i]);
                    else
                        printf(" %d",primes[i]);
                    temp=temp/primes[i];


                }
            }

        }
        if(temp!=1){

                printf(" %d",temp);

        }
            printf("\n");

    }


    return 0;
}

Re: 583 - Prime Factors

Posted: Fri Mar 10, 2017 12:40 pm
by lighted
Problem description says
For an input number g > 0, g = f1 × f2 × · · · × fn, where each fi is a prime number greater than unity (with fi ≤ fj for i < j),
the format of the output line should be g = f1 x f2 x . . . x fn
For sample input

Code: Select all

-190
-194
196
198
0
Your output is

Code: Select all

-190 = -1 x 5 x 19 x 2
-194 = -1 x 97 x 2
196 = 7 x 7 x 4
198 = 3 x 3 x 11 x 2
Correct output should be

Code: Select all

-190 = -1 x 2 x 5 x 19
-194 = -1 x 2 x 97
196 = 2 x 2 x 7 x 7
198 = 2 x 3 x 3 x 11