## 160 - Factors and Factorials

Moderator: Board moderators

ugrash
New poster
Posts: 5
Joined: Fri Apr 01, 2005 2:06 pm

I found my mistake (using this forum, i'm ashamed)

when you have 15 prime factors, you have an excessive "\n" ....

murkho
New poster
Posts: 33
Joined: Mon Mar 28, 2005 6:41 pm

### WA -160(Factors and factorial)

Hi,
Can anyone help me to show the bug of my coding.
Here is the coding for consideration....

Code: Select all

``````//Name:factors and factorials..
//no:160.......................

#include<stdio.h>
#include<memory.h>

int main()
{
int prime[110] = {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 in,i,j,t,m;
int count[110];
for(i = 0;i<100;i++)
count[i] = 0;

while(scanf("%d",&in) != EOF)
{
memset(count,0,sizeof(count));
if(in == 0)
break;
for(i = 0;prime[i]<=in && prime[i];i++)
{

t = in;
for(j = prime[i];t>=j;)
{
count[j] +=  t/prime[i];
t = t/prime[i];

}

}

printf("%3d! =",in);
for(m = 0; m<i;m++)
{
printf("%3d",count[prime[m]]);
if((m+1) %15 == 0)
printf("\n      ");

}

}

return 0;
}
``````

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore
Try with this small input:

Input:
8!
7!

You get your output for both joined together.

murkho
New poster
Posts: 33
Joined: Mon Mar 28, 2005 6:41 pm

### 160 (factors and factorials) WA

When first time I saw WA in this board i thought it's a way of sharing delight.Now i know this is an error. I have one of this error in problem 160.Can anybody help me to find out the bug? Or can anybody send me some input-output set?

Code: Select all

``````//Name:factors and factorials..
//no:160.......................
#include<stdio.h>
#include<memory.h>
int main()
{
int prime[110] = {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 in,i,j,t,m;
int count[110];
for(i = 0;i<100;i++)
count[i] = 0;
//freopen("c:\\160.txt","r",stdin);
//calculating factors here.
while(scanf("%d",&in) != EOF)
{
memset(count,0,sizeof(count));
if(in == 0)
break;
for(i = 0;prime[i]<=in && prime[i];i++)
{
t = in;
for(j = prime[i];t>=j;)
{
count[j] +=  t/prime[i];
t = t/prime[i];
}
}
//printing the result...
printf("%3d! =",in);
for(m = 0; m<i;m++)
{
printf("%3d",count[prime[m]]);
if((m+1) %15 == 0)
printf("\n      ");
}
printf("\n");
}
return 0;
}``````

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
Your program prints unnecessary empty line when 47 <= n <= 52.

45793HM
New poster
Posts: 4
Joined: Tue Jun 14, 2005 4:49 pm
Location: Portugal

### Can't find error in Problem 160

Can someone help me.
My code:
#include <stdio.h>
int main(void)
{ int i,j,k,n,soma[25],p[]={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};

scanf("%d",&n);
while(n>=2)
{ if ((n>=2) && (n<=100))
{ for (i=0;i<26;i++) soma=0;

for (i=2;i<=n;i++)
{
j=i;
k=0;
while (j>1)
if ((j%p[k])==0)
{j/=p[k];
soma[k]++;}
else
k++;
}

i=25;
while(soma[i--]==0);
i++;
printf("%3d! =",n);
for (j=0;j<=i;j++)
{
if (((j%15)==0) && (j>0)) printf("\n "); /* there are 6 spaces */
printf("%3d",soma[j]);
}
printf("\n");
}
scanf("%d",&n);
}
}

Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET
What's acutally wrong? Did you recieved a WA or something else or just couldn't managed to match the sample I/O? I tested your code & it printed

Code: Select all

``````0! =
``````
for all the inputs.
Btw, why do you adopt this peculiar way to handle multiple cases? I wonder whether it really works or not. You may use this

Code: Select all

``````while(scanf(" %d",&n)==1)
{
if(!n)
break;

//.... do some processing

}``````
You should never take more than you give in the circle of life.

45793HM
New poster
Posts: 4
Joined: Tue Jun 14, 2005 4:49 pm
Location: Portugal

### Allway's WA error, the exact problem in Pascal works fine

I've modified the program to this and it does'nt work.
i have check all the results form 2 to 100 , all are good
i try several inputs, and nothing.
i stil don't understand

#include <stdio.h>
int main(void)
{ int i,j,k,n,soma[25],p[25]={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};

while(scanf(" %d",&n)==1)
{
if (!((n>=2) && (n<=100))) break;

for (i=0;i<26;i++) soma=0;

for (i=2;i<=n;i++)
{
j=i;
k=0;
while (j>1)
if ((j%p[k])==0)
{j/=p[k];
soma[k]++;}
else
k++;
}
i=25;
while(soma[--i]==0);

printf("%3d! =",n);
for (j=0;j<=i;j++)
{
if (((j%15)==0) && (j>0)) printf("\n ");
printf("%3d",soma[j]);
}
printf("\n");
}
}

Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET

### Re: Allway's WA error, the exact problem in Pascal works fin

Well, you've eventually compelled me to do some debugging, Anyway,
You've used the following code block for initialization -

Code: Select all

``````for (i=0;i<26;i++) soma[i]=0;
``````
That means your program accesses #0 to #25 index of that array i.e. it requires at least 26 cells. But you've declared -

Code: Select all

``````soma[25]
``````
So, array goes out of bound & overwrites something else. While debugging your program I found that every time the initialization loop ends, n turns to 0. Then the rest of the program could happily consider it's job done by printing

Code: Select all

``````0! =
``````
for any input. You are lucky (or unlucky, whichever you think ) that the variable n was not overwritten while running in your pc. But you never know, how it will behave while running in judge. There is every chance that it'll go the wrong way. So, be carefull to allocate sufficient memory.

Finally, when your output goes to second line, is it correctly formatted? I'll bet for the negative.

/* Hint: use printf("%9d"....) in the second line */
You should never take more than you give in the circle of life.

45793HM
New poster
Posts: 4
Joined: Tue Jun 14, 2005 4:49 pm
Location: Portugal

### Thanks - it works

Thanks a lot.

now i can sleep better.

Experienced poster
Posts: 154
Joined: Sat Apr 17, 2004 9:34 am
Location: EEE, BUET
Nice to know that it finally works . It's behaviour surprised me, too.
You should never take more than you give in the circle of life.

Tanu
Learning poster
Posts: 70
Joined: Sun May 29, 2005 12:46 pm
Location: Mars

### The judge must gone mad with 160

Code: Select all

``````#include<stdio.h>
#include<math.h>
#define N 101
main()
{
int
i,j,c,record[N+5][30],max[27],count,input,primes[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,61,67,71,73,79,83,89,93,97};

for(i=0;i<N;i++)
for(j=0;j<25;j++)
record[i][j]=0;

for(i=0;i<25;i++)
max[i]=0;

for(j=1;j<=N;j++)
{
c=j;
for(i=0;i<25;i++)
{
record[j-1][i]=max[i];
while((c%primes[i])==0)
{
record[j-1][i]++;
c/=primes[i];
}
max[i]=record[j-1][i];
}
}
while(1)
{
scanf("%ld",&input);
if(input==0)
break;
printf("%3ld! =",input);
count=0;
for(i=0;i<25;i++)
{
if(record[input-1][i]==0)
{

}
else
{
count++;
printf("%3ld",record[input-1][i]);
}
if(count>14)
{
count=0;
printf("\n      ");
}
}
printf("\n");
}
}
``````
I show nothing Wrong(It may be my less eye power)...
Bue tried it 6 times...
Plz go through once my code...

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore
The judge is not wrong in judging your code WA.

After reading your code I found one problem. Are you sure you have all the prime numbers between 1 and 100 in your primes array? I see one missing.

Tanu
Learning poster
Posts: 70
Joined: Sun May 29, 2005 12:46 pm
Location: Mars

### Thanx

Thanx a lot...
It was my fool...
Now I got acc...
Good luck...
Thanks again...
Bye

smilitude
Experienced poster
Posts: 137
Joined: Fri Jul 01, 2005 12:21 am

### 160 - WA

hmm... only i know in this whole world... what a great collection of WA i got! let me share one of those...

Code: Select all

``````/*
160 factors and factorials
submission 1 WA
coded at 5:51am oct 14,05

i once got an infinite loop here coz, i divided the loop constant(i) instead of number!
ha ha ha!

i corrected, then submitted to get a wa...
*/
CUTTTTTTTTTTTTTTTTTTTTTT!!!
``````
Last edited by smilitude on Sun Nov 27, 2005 7:59 pm, edited 1 time in total.
fahim
#include <smile.h>