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

opportunity
New poster
Posts: 9
Joined: Thu Sep 15, 2005 11:35 pm
Location: dhaka

160: giving WA

Post by opportunity » Tue Feb 28, 2006 8:36 pm

i am getting WA but unable to sort out where error occurred. plz someone help me out.

Code: Select all

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

int primes[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}; 

int main()
{
	int index = -1;
	int N,numOfPrime,multipleOfPrime;
	int i,k,j;
	while(scanf("%d",&N)==1)
	{
		if(N==0)
			break;
		for(i=0;i<25;i++)
		{
			index = -1;
			if(primes[i]>=N)
			{
				index = i;
				break;
			}
		}

		printf("%3d! =",N);

		for(j=0;j<=index;j++)
		{
			numOfPrime = 0;
			multipleOfPrime = 1;

			for(k=0;multipleOfPrime<=N;k++)
			{
				multipleOfPrime*=primes[j];
				numOfPrime+=(int)floor(N/multipleOfPrime);
			}
			if(numOfPrime)
			{
				if(j&&(j%15)==0)
				{
					printf("\n%9d",numOfPrime);
					continue;
				}
				/*else*/ 
					printf("%3d",numOfPrime);
			}
		}
		printf("\n");
	}

	return 0;
}

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Tue Feb 28, 2006 10:25 pm

Look what happens when the input value is bigger than 97.
The biggest problem with most problems is not how to solve the problem, but how to not solve what is not the problem.

opportunity
New poster
Posts: 9
Joined: Thu Sep 15, 2005 11:35 pm
Location: dhaka

thanks guru

Post by opportunity » Wed Mar 01, 2006 9:23 pm

thank u so much. i was missing a checking condition. now it is accepted

sds1100
Learning poster
Posts: 95
Joined: Sat Dec 10, 2005 2:09 pm

o_o

Post by sds1100 » Fri Mar 03, 2006 8:24 am

oo

sds1100
Learning poster
Posts: 95
Joined: Sat Dec 10, 2005 2:09 pm

Post by sds1100 » Fri Mar 03, 2006 8:43 am

sorry i don't know your program..
sorry.sorry.sorry.sorry.sorry.

LemonMint
New poster
Posts: 1
Joined: Mon Mar 13, 2006 10:28 am

160 Help Me!! << C

Post by LemonMint » Mon Mar 13, 2006 10:31 am

#include <stdio.h>
int Pri[100];

void PrimeC(int a)
{
int i;

for(i=2;;i++){
if( a % i == 0 ){
while(a % i == 0){
a=a/i;
Pri++;
}
}
if( a == 1 ) break;
}
}

void Clear(){
int i;
for(i=1;i<=100;i++)
Pri=0;
}

void main()
{
int i,j,n,tmp,cnt=0,num=0;

while(scanf("%d",&n))
{
if( n == 0 ) break;
for(i=2;i<=n;i++) PrimeC(i);

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

for(i=2;i<=n;i++){
tmp=i;
for(j=1;j<=i-1;j++){
if( tmp % j == 0 ) cnt++;
if( cnt > 1 ) break;
}
if( cnt > 1 ) cnt=0;
else{
printf("%3d",Pri);
num++;
if( num % 15 == 0 ) printf("\n ");
cnt=0;
}
}
Clear();
if( num % 15 != 0 ) printf("\n");
else printf("\b\b\b\b\b\b");
num=0;
}
}

I don't know what's wrong.
Plz help me !!! :oops:

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko » Mon Mar 13, 2006 10:51 am

Maybe it's:

int Pri[100];

and

Pri[100] = 0; ?

Darko

f.eliel
New poster
Posts: 10
Joined: Wed Feb 08, 2006 4:31 pm

160 - Help!

Post by f.eliel » Fri Mar 24, 2006 9:09 pm

I have 3 problems with my code:

When i test with the sample input 53, it doesn't work (it seems that unsigned long int isn't big enough, but i don't know how to fix it).

When i test whit 7 or more numbers it doesn't work and i don't know why.

And i don't know how to jump to the next line when the output passes 15 numbers.

Here is my code:

Code: Select all

#include <stdio.h>

int count[25];
int y;
void prime(unsigned long a);

int main()
{
   int i = 0, w, x, j, n[i];
   unsigned long fat[i];
       
   while (scanf("%d", &n[i]) == 1) {
      if (n[i] == 0) break;
      i++;
   }
   w = 0;
   while (w < i) {
      fat[w] = n[w];
      j = (fat[w]-1);
      while (j > 1) {
         fat[w] *= j;
         printf("%lu ", fat[w]);
         j--;
      }
      prime(fat[w]);
      printf("%3d! =", n[w]);
      for (x = 0; x <= y; x++)
         printf("%3d", count[x]);
      printf("\n");
      w++;
   }
   return 0;
}         

void prime(unsigned long a)
{
   int x;
   int primes[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, 93};
   
   for (x = 0; x < 25; x++)
      count[x] = 0;
   for (x = 0;; x++) {
      while (!(a % primes[x])) {
         a /= primes[x];
         count[x]++;
      }
      if (a == 1) {
         y = x;
         break;
      }
   }
}
Thanks for any help you can give.

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Sat Mar 25, 2006 8:48 am

I see a lot of problems in your program.

1) You have a prime number < 100 that is missing in your primes array.
2) Why are you declaring an array n of size 0 since i is initialized to 0 and you declare int n on the same line of code? You are going to get runtime error this way. Why don't you process input as you read it instead of using an array to hold the input?
3) There is no need to compute the factorial even though the input can be as large as 100.
4) To go to the next line after 15 numbers, you can use a counter to keep track of how many numbers you have printed.

Read past threads on this problem to get some hints on solving this problem.

ayeshapakhi
Learning poster
Posts: 60
Joined: Sun Apr 16, 2006 7:59 pm

160 WA

Post by ayeshapakhi » Tue May 30, 2006 10:42 am

dear fellows......
pls help abt 160...it would be better if u can provide me some test cases...
or if there's any mistake abt the formatting pls help me fixing....

Code: Select all

#include <stdio.h>
#define size 25

void main()
{
	//freopen("in.txt","r",stdin);
	//freopen("Mout.txt","w",stdout);

	long int prime[size]={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};
	long int counter[size],number,i,j,temp;

    while (scanf("%ld",&number)==1 && number)
	{
		for(i=0; i<size; i++) counter[i]=0;

		for(j=number; j>1; j--)
			for(i=0; i<size; i++)
			{ 
				temp=j;
				while(temp%prime[i]==0)
				{
					counter[i]++;
					temp=temp/prime[i];
				}
			}

			printf("%3ld! =",number);
			for(i=0; i<size; i++)
			{
				if (prime[i]>number) break;
				printf("%3ld",counter[i]);

				if( i==14)printf("\n      ");
			}
			printf("\n");
	}
}

thanks a lot..........

User avatar
dumb dan
Learning poster
Posts: 67
Joined: Tue Aug 05, 2003 1:02 am

Post by dumb dan » Tue May 30, 2006 2:10 pm

Try this input and see if you can spot your error (and don't forget to remove your code after you get AC).

input:

Code: Select all

45
47
49
0

sohel_acm
New poster
Posts: 8
Joined: Fri May 19, 2006 11:27 pm
Location: Bangladesh

Post by sohel_acm » Tue May 30, 2006 10:23 pm

Hi
You should change your code as follow

Code: Select all

for(i=0; i<size; i++) 
         { 
             if(i==15) printf("\n      ");
            if (prime[i]>number) break; 
            printf("%3ld",counter[i]); 

             
         }
 
Unless your program will creat an extra line in special cases.
Good Luck.
Love programmers,help programmers.

kwedeer
New poster
Posts: 44
Joined: Thu Dec 15, 2005 11:28 pm

Post by kwedeer » Sat Jun 03, 2006 4:17 pm

I get presentation error for my solution (yet - it appears on accpeted list of my profile - but anyway - I would feel better if I got accepted without presentation error) - so - help me please:

here is my code:
code delete upon request.

and here the output for all the possible test cases (so - what's wrong?):
all the possible answers deleted upon request

btw - here is Mathematica routine for answer check:
FactorInteger[100!]
Last edited by kwedeer on Sat Jun 03, 2006 5:54 pm, edited 1 time in total.

kwedeer
New poster
Posts: 44
Joined: Thu Dec 15, 2005 11:28 pm

Post by kwedeer » Sat Jun 03, 2006 4:19 pm

well - I see that - at least - this board software delete the blank spaces in if they are the first symbols in line... Maybe - judge does the same and this is the cause of PE? Is so - can You suggest how to code these blank spaces in Pascal to be perceived normally?

Thanx in advace!

small addition - my result file can be read by notepad or wordpad as one that is presented as required by task... So - I hope that Judge is at least as smart as MS :)))

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Sat Jun 03, 2006 4:44 pm

Your last line of output should also end in a return, so add an extra writeln at the end of your code.
To prevent the board from skipping leading spaces, place your output text between code tags, just as you did with your program.

Your code and your output are spoilers for this problem. Please delete them.

Post Reply

Return to “Volume 1 (100-199)”