Code: Select all
Thank you brianfry713, got an AC
Moderator: Board moderators
Code: Select all
Thank you brianfry713, got an AC
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 ");
}
}
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;
}
For sample inputFor 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
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
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