## 583 - Prime Factors

Moderator: Board moderators

pinou
New poster
Posts: 2
Joined: Thu Oct 16, 2014 6:18 pm

### Re: 583 - Prime Factors

I get a WA with that code, can anyone tell me why:

Code: Select all

``````Thank you brianfry713, got an AC
``````
Last edited by pinou on Sat Oct 18, 2014 12:18 am, edited 2 times in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 583 - Prime Factors

Try running your code on the sample input.
Check input and AC output for thousands of problems on uDebug!

pinou
New poster
Posts: 2
Joined: Thu Oct 16, 2014 6:18 pm

### Re: 583 - Prime Factors

I tested the sample input the answers are correct.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 583 - Prime Factors

Don't print a space at the end of the line.

To read and print a long, use %li or %ld instead of %i.
Check input and AC output for thousands of problems on uDebug!

prokawsar
New poster
Posts: 7
Joined: Tue Aug 04, 2015 5:01 pm

### Re: 583 - Prime Factors

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 ");
}
}
``````

ainan.ahmed
New poster
Posts: 2
Joined: Sun Jan 08, 2017 12:45 pm

### Re: 583 - Prime Factors

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;
}
``````

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

### Re: 583 - Prime Factors

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``````

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
``````
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman