## 160 - Factors and Factorials

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Kamanashish
New poster
Posts: 10
Joined: Wed Dec 17, 2003 3:12 pm
Location: Dhaka
Contact:

### 160 WA help plz.

I got WA.PLZ help.

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

long a[110];

void FF(long x)
{

long c;

c=2;
while(1)
{
while(1)
{
if(x%c==0)
{
a[c]++;
x=x/c;
}
else break;
}

if(x==1)break;

if(c>=sqrt(x))
{
a[x]++;
break;
}

c++;

}
}

void main()
{
long n,i,j;
while(scanf("%ld",&n))
{
if(n==0)break;
printf("%3ld! =",n);

for(i=0;i<110;++i)a=0;

for(i=2;i<=n;++i)
{
FF(i);
}
j=0;
for(i=2;i<=n;++i)
{
if(a>0)
{
j++;
if(a>=10)printf("%3ld",a);
if(a<10)printf("%3ld",a);
if(j%15==0)printf("\n ");
}

}

printf("\n");

}
}
Work hard.

Md. Azam Khan
New poster
Posts: 8
Joined: Tue Jun 15, 2004 7:16 pm
Contact:
Hellow,
Read the problem description carefully.
1. Each line contain fifteen numbers. No new line will be when just fifteen number should print.
2. From the 16th numbur u should give a new line with six extra space for sixteenth number.
3. What will be output when n=1.

Wish these will help you . Thanks.

=>. if(a>0)
{
j++;
if(j%16==0)printf("\n "); //six spaces after new line
if(a>=10)printf("%3ld",a);
if(a<10)printf("%3ld",a);
}
=>. if(n>1) printf("%3ld! =",n);// may be need not
=> try to write in this style while(scanf("%ld",&n)==1)//with return value

I born to code .

oulongbin
Learning poster
Posts: 53
Joined: Sat Jul 10, 2004 5:57 pm
Location: Shanghai China

### WA 160 Why ?

this is my code :
[cpp]
#include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
bool prime(int n)
{

int j,k;
k=int(sqrt(n));
for(j=2;j<=k;j++)
if(n%j==0)break;
if(j>k) return true;
else return false;

}

int main()
{
int prim[100];
int n;
int count;
int i;
bool flag;
while(cin>>n&&n!=0)
{
for(i=0;i<100;i++)
prim=0;

count=0;
printf("%3d! =",n);

for(i=2;i<=n;i++)
{
flag=true;
if(prime(i))
prim++;
else
{
int temp=i;

for(int j=2;j<i;j++)
{
if(prime(j))
{
while(temp%j==0)
{
prim[j]++;
temp/=j;
if(prime(temp))
{
prim[temp]++;
flag=false;
break;
}
}
}
if(!flag)
break;
}
}
}

for(i=0;i<100;i++)
{
if(prim!=0)
{
printf("%3d",prim);
count++;
}
if(count==15)
{
cout<<endl;
count=0;
cout<<" ";
}
}
cout<<endl;
}

return 0;

}
[/cpp]

Andrew Neitsch
New poster
Posts: 43
Joined: Fri Jun 25, 2004 9:37 pm
If you post your algorithm in English, I will help you.

oulongbin
Learning poster
Posts: 53
Joined: Sat Jul 10, 2004 5:57 pm
Location: Shanghai China
I know why i was wrong.
It's the output problem,thank you .

bangla
New poster
Posts: 5
Joined: Sun Sep 19, 2004 10:22 am

I have got WA for the problem 160 (factors and factorials). Please tell me what is wrong with my code? Here is my source code.
[c]
#include<stdio.h>
#include<string.h>
void main()
{
int primes[]={ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,
53,59,61,67,71,73,79,83,89,97 };
int factors[30],N,n,k,sum;
while(scanf("%d",&n)==1)
{
int i=0;
if (n==0) break;
while(primes<=n)
{
N=n;sum=0;
do{
k=N/primes;
sum=sum+k;
N=k;
}while(k>1);
factors=sum;
i++;
if (i>=25) break;
}
printf("%3d! =",n);
for(int j=0;j<i;j++)
{
printf("%3d",factors[j]);
if(j==14) printf("\n ");
}
printf("\n");
}
}
[/c]

bangla
New poster
Posts: 5
Joined: Sun Sep 19, 2004 10:22 am

I have got WA for the problem 160 (factors and factorials). Please tell me what is wrong with my code?
[c]
#include<stdio.h>
#include<string.h>
void main()
{
int primes[]={ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,
53,59,61,67,71,73,79,83,89,97 };
int factors[30],N,n,k,sum;
while(scanf("%d",&n)==1)
{
int i=0;
if (n==0) break;
while(primes<=n)
{
N=n;sum=0;
do{
k=N/primes;
sum=sum+k;
N=k;
}while(k>1);
factors=sum;
i++;
if (i>=25) break;
}
printf("%3d! =",n);
for(int j=0;j<i;j++)
{
printf("%3d",factors[j]);
if(j==14) printf("\n ");
}
printf("\n");
}
}
[/c]

Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela
Hi!!! bangla the error was dificult to find but I finally made I dont say to you the error I give you the inputs for you code give the bad answer

Input:

Code: Select all

``````
46
47
48
49
50
51
52
53
``````

Code: Select all

`````` 46! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1
47! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1  1

48! = 46 22 10  6  4  3  2  2  2  1  1  1  1  1  1

49! = 46 22 10  8  4  3  2  2  2  1  1  1  1  1  1

50! = 47 22 12  8  4  3  2  2  2  1  1  1  1  1  1

51! = 47 23 12  8  4  3  3  2  2  1  1  1  1  1  1

52! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1

53! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1
1
``````
My ouput:

Code: Select all

`````` 46! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1
47! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1  1
48! = 46 22 10  6  4  3  2  2  2  1  1  1  1  1  1
49! = 46 22 10  8  4  3  2  2  2  1  1  1  1  1  1
50! = 47 22 12  8  4  3  2  2  2  1  1  1  1  1  1
51! = 47 23 12  8  4  3  3  2  2  1  1  1  1  1  1
52! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1
53! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1
1
``````
if change that give you AC!!
Hope its Helps
Keep posting!!!

New poster
Posts: 8
Joined: Fri Sep 24, 2004 8:40 am
i am getting the correct answer on my comp but the judge said it gave a WA ,why?
here's my code:
#include<iostream>
#include<queue>

using namespace std;

void gen_prime(queue<int> &p,int n)
{
int i=2,j;
while(i<=n)
{
for(j=2;j<i;j++)
{
if(i%j==0) break;
}
if(i==j) p.push(i);
i++;
}
}

int powr(int b,int e)
{
int p=1;
for(int i=0;i<e;i++)p=p*b;
return p;
}

void factors(int n)
{
int flr=0,j;
queue<int> p,q;
gen_prime(p,n);
while(!p.empty())
{
// cout<<"a"<<endl;
j=p.front();
flr=0;
for(int i=1;powr(j,i)<=n;i++)
{
flr=flr+(n/powr(j,i));
}
q.push(flr);
p.pop();
}
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}
}

int main()
{
int n;
cin>>n;
while(n!=0)
{
cout<<n<<"! = ";
factors(n);
cout<<endl;
cin>>n;
}
return 0;
}

Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela
Hi!! vladimir manich i found this bug in your code
Input

Code: Select all

``````90
67
47
``````

Code: Select all

``````90! = 86 44 21 13 8 6 5 4 3 3 2 2 2 2 1 1 1 1 1 1 1 1 1 1
67! = 64 31 15 10 6 5 3 3 2 2 2 1 1 1 1 1 1 1 1
47! = 42 21 10 6 4 3 2 2 2 1 1 1 1 1 1
``````
My ouput from my AC code :

Code: Select all

``````90! = 86 44 21 13  8  6  5  4  3  3  2  2  2  2  1
1  1  1  1  1  1  1  1  1
67! = 64 31 15 10  6  5  3  3  2  2  2  1  1  1  1
1  1  1  1
47! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1  1
``````
try to check again the output format in the problem specification
Hope its Helps
Keep posting!!

New poster
Posts: 8
Joined: Fri Sep 24, 2004 8:40 am
thankyou

bangla
New poster
Posts: 5
Joined: Sun Sep 19, 2004 10:22 am
Thank you for your help.
I have modified my code accordingly. But the judge said it gave compile error. Why? What does it mean? My program generate the output as:
input:

Code: Select all

``````
46
47
48
100
49
50
51
90
67
52
53
46
0
``````
output:

Code: Select all

`````` 46! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1
47! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1  1
48! = 46 22 10  6  4  3  2  2  2  1  1  1  1  1  1
100! = 97 48 24 16  9  7  5  5  4  3  3  2  2  2  2
1  1  1  1  1  1  1  1  1  1
49! = 46 22 10  8  4  3  2  2  2  1  1  1  1  1  1
50! = 47 22 12  8  4  3  2  2  2  1  1  1  1  1  1
51! = 47 23 12  8  4  3  3  2  2  1  1  1  1  1  1
90! = 86 44 21 13  8  6  5  4  3  3  2  2  2  2  1
1  1  1  1  1  1  1  1  1
67! = 64 31 15 10  6  5  3  3  2  2  2  1  1  1  1
1  1  1  1
52! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1
53! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1
1
46! = 42 21 10  6  4  3  2  2  2  1  1  1  1  1
``````

bangla
New poster
Posts: 5
Joined: Sun Sep 19, 2004 10:22 am
Thank you Ghust_omega.
Finally I got AC!!

Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela
Hi bangla, if code not change much, I think that the error is that you can not declare a int in the midle of the code here

Code: Select all

``````for(int j=0;j<i;j++
``````
change that
Hope it Helps
Keep posting

bangla
New poster
Posts: 5
Joined: Sun Sep 19, 2004 10:22 am