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

imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Post by imranul » Mon Jul 22, 2002 3:02 pm

There are two problems:
i) There is an extra space after the '=' sign in each step and also when you printed the newline character along with some spaces.

Code: Select all

here---->>printf("%3d! = ", fact);
      for (i = 0; i < 25; i++) {
         if (i == 15)
here----->>printf("\n       ");  
 if (factors[i] == 0)
            break;
         printf("%3d", factors[i]);
      }
printf("\n");
ii) There is also a problem when the out put contains exactly 15 numbers after the '=' sign. Examine your code. Suppose there are 15 numbers in the output. then i==15 is satisfied and a newline is printed. But as there are not more then 15 numbers in the output no number is printed in the next line. And for the last "printf("\n");" there is another newline in the output. So for certain numbers there will be 2 newlines.

What u can do is just put the if(i==15) condition after the if(factors ==0)
condition.

8)
Life is like a box of Chocolates,
you never know what you're going to get...

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

accepted thx

Post by ec3_limz » Tue Jul 23, 2002 9:47 am

Got accepted. Thanx a lot! :D

razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

160:Always WA..why?

Post by razibcse » Tue Aug 06, 2002 2:05 am

Please tell me why it's Wrong answer....I did it in C

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

void main()
{
long t,cn[30],p,pr[30],a,b,c,d,e,n,i,j;

for(;;)
{
scanf("%ld",&n);
if(n==0) break;
d=0;
for(a=2;a<=n;a++)
{
p=1;
b=a;
for(c=2;c<=sqrt(b);c++)
if(!(b%c))
{
p=0;
break;
}
if(p)
pr[d++]=b;
}
e=d;

for(d=0;d<e;d++)
cn[d]=0;

for(a=n;a>=2;a--)
{
b=a;
for(d=0;d<e;)
{
i=0;
if(!(b%pr[d]))
{
t=d;
b/=pr[d];
cn[t]++;
i=1;
}

if(b<pr[d]) break;
if(!i) d++;
}
}
printf("%3ld! =",n);
for(j=0;j<e;j++)
{
printf("%3ld",cn[j]);

if(j==14)
{
printf("\n");
printf(" ");
}

}
printf("\n");
}
}

shohag sust cse
New poster
Posts: 4
Joined: Tue Aug 06, 2002 11:38 am

p-160 by shohag

Post by shohag sust cse » Wed Aug 07, 2002 8:26 am

I think this is not good.
There is a good process.Like,
Below or equal 5 all prime are 2,3,5.
and for 5 the power of 2 is

k1=5/2;
k2=2/2;
sum=k1+k2;

total power of 2 is 3.
This process for 2,until ki<=2.
Same process for 3,5.

So, for 5

5!=2^3*3^1*5^1;

Is this a good process.
:lol:

imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Post by imranul » Wed Aug 07, 2002 5:27 pm

Hi Razib....

There's nothing wrong about the method....u've done wrong with the printing.

Code: Select all


for(j=0;j<e;j++) 
{ 
printf("%3ld",cn[j]); 

if(j==14) 
{ 
printf("\n"); 
printf(" "); 
}

look at ur code...there are 2 mistakes:

i) when the output goes to the 2nd line u've printed less spaces
(i'm not sure about it coz as u paste ur code here sometimes the spaces vanish. so u should place ur code within code tag )

ii) Think of the case when e equals to 15. In that case when j=14 u've printed a newline and a space...and then the loop terminates and after that u've printed another newline. So for certain numbers there are two newlines (47 to 52)

what u can do is replace the if(j==14) part and do it in the folllowing way:

Code: Select all


for(j=0;j<e;j++) 
{ 
if(j==15) 
{ 
printf("\n"); 
printf("      "); 
}

printf("%3ld",cn[j]); 

}
Life is like a box of Chocolates,
you never know what you're going to get...

razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

Thanx for ur support !!!

Post by razibcse » Thu Aug 08, 2002 9:00 pm

Hi,
thanx a lot for ur sujession...
I got the problem 160 accepted b4 i received ur reply...
I had to cut the spaces just...
Then everything was fine...

Thanx again...
Razib :lol:

hectorzeta
New poster
Posts: 4
Joined: Mon Jun 23, 2003 1:54 am

I need help. I'm trying to resolve problem 160 in java.

Post by hectorzeta » Mon Jun 23, 2003 2:22 am

Hello :D :

I'm new here, and I have been having troubles using the judge: first because I couldn't use java.oi and after because I got a Wrong answer :( when I'm really sure my solution works.

So I give you my java code and I hope some of you could help me.

[java]
// @JUDGE_ID: 32852WA 160 Java
import java.io.*;
import java.util.*;

class Main{

int ajuste = 0;
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}

public static void main (String args[]) throws IOException{
Main funciona = new Main();
funciona.empieza();
}

void empieza(){

String input;
while ((input = Main.ReadLn (255)) != null) {
String linea = input;
StringTokenizer st = new StringTokenizer(linea);
int num = Integer.parseInt(st.nextToken());
ajuste = 0;
if(!(num == 0 || num < 2 || num > 100)){
if(num == 100 ){
System.out.print(num + "!" + " = ");
}else if(num < 100 && num >= 10){
System.out.print(" " + num + "!" + " = ");
}else{
System.out.print(" " + num + "!" + " = ");
}
int primo = 2;
do{
if(primo != 2 && ajuste != 15)
System.out.print(" ");
int veces = cuentaVeces(num, primo);
imprimeVeces(veces);
primo = nextPrimo(primo);
}while(num >= primo);
}else{
break;
}
}
}

int nextPrimo(int n){
int posible = n + 1;
while(true){
for(int i = 2; i < posible; i++){
if(posible % i == 0){
posible++;
i = 2;
}
}
return posible;
}
}

void imprimeVeces(int veces){
if(ajuste == 15){
System.out.print("\n ");
System.out.flush();
ajuste = 0;
}
if(veces < 100 && veces >= 10){
System.out.print(veces + "");
}else{
System.out.print(" " + veces + "");
}
ajuste++;
}

int cuentaVeces(int num, int primo){
int aux = 0;
int primoAux = primo;
while(num >= primo){
aux += num/primo;
primo *= primoAux;
}
return aux;
}
}
[/java]

The messange that I received was:
"Your program has not solved the problem. It ran during 0.008 seconds."
but why??
Last edited by hectorzeta on Mon Jun 23, 2003 3:29 am, edited 1 time in total.

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

Post by chunyi81 » Mon Jun 23, 2003 3:21 am

Are u trying to print out the prime factors using the code u posted? I think the problem is asking u to print the number of '2's, '3's, and so on that appears in n!, where n is a number. Read the problem description again.

Here's what I did to solve this problem: Dynamic programming and a single-dimension array will do the job.

In fact, u can import java.io for the IOException class, but u cannot use any of the other classes there.

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

Re: I need help. I'm trying to resolve problem 160 in java.

Post by chunyi81 » Mon Jun 23, 2003 3:28 am

hectorzeta wrote:Hello :D :

[java]
void imprimeVeces(int veces){
if(ajuste == 15){
System.out.print("\n "); System.out.flush();
ajuste = 0;
}
if(veces < 100 && veces >= 10){
System.out.print(veces + " ");
}else{
System.out.print(" " + veces + " ");
}
ajuste++;
}

}[/java]

The messange that I received was:
"Your program has not solved the problem. It ran during 0.008 seconds."
but why??
There is also a problem I saw with the statement in bold, why are you doing this: System.out.print("\n "); ? U will add extra spaces in the next line. Use the following instead: System.out.println();

Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

Why 160 P.E.

Post by Zhao Le » Sat Jun 28, 2003 4:53 am

I solved this problem in 0.0s
but still P.E.

Who can help me?

I don't understand.

Thanks in advance.

[cpp]#include <iostream>
#include <iomanip.h>

int const Max=25;
int Prime[Max];
int Count[Max]={0};

void CreatePrime(int max)
{
int k=0;
int i,j;
for(i=2;i<max;i++)
{
for(j=i-1;j>1;j--)
if(i%j==0) break;
if(j==1)
{
//cout<<i<<" ";
Prime[k++]=i;
}
}
}

void main()
{
CreatePrime(100);
int n; // range from 2 to 100
while(1)
{
cin>>n;
if(n==0) break;
setiosflags(ios::right);
cout<<setw(3)<<n<<"! =";
int i,j;
for(i=n;i>1;i=n)
{
for(j=0;i>1&&j<Max;j++)
if(i>=Prime[j]&&i%Prime[j]==0)
{
i/=Prime[j];
Count[j]++;
j--; // continue test the current number
}
n--;
}
for(i=0;i<Max;i++)
if(Count==0) break;
j=i;
int k;
for(i=0,k=0;i<j;i++,k++)
{
if(k==15)
{
cout<<endl<<" ";// 6 spaces
k=0;
}
setiosflags(ios::right);
cout<<setw(3)<<Count<<" ";
Count=0; // Clear the early stage
}
cout<<endl;
}
}[/cpp]

Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

Post by Zhao Le » Sun Jun 29, 2003 10:39 am

I know what's wrong with my code.

Thank you all visitors.

Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post by Maarten » Fri Oct 31, 2003 3:09 pm

This thread helped me too, i was printing newline accidently. But why it gives WA? shouldn't it give Presentation Error ?

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan » Sat Nov 08, 2003 3:21 pm

Well Maarten,
maybe you should post your code so I can examine it!! :wink: :wink: :wink:
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

160 wa help me plz

Post by problem » Tue Nov 25, 2003 11:47 am

wa.think its very simple.every input output is ok.plz help me.

[cpp]

/* @JUDGE_ID: xxxxxx 160 C++ */


#include<stdio.h>
int main()
{
long int i,j,n,k,p=0;
static int x[100];
scanf("%ld",&n);
while(n>0)
{ for(i=2;i<=n;i++)
{ j=i;
for(k=2;k<=i;k++)
{ while(j%k==0)
{ ++x[k];
j/=k;
}
}
}
printf("%3ld! = ",n);
for(i=0;i<=99;i++)
{
if(x)
{
p++;
printf("%3d ",x);
}
if(p%15==0&&p>0)
{
if(n==100)
printf("\n ");
else
printf("\n ");
p=0;
}
}
for(i=0;i<=99;i++)
x=0;
printf("\n");
scanf("%ld",&n);
//if(n==100)
//printf(" ");
p=0;
}
return 0;
}
[\cpp]

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

PE why ?help me.

Post by problem » Wed Dec 10, 2003 11:03 am

PE help me plz.here is my source code.

/* @JUDGE_ID: xxxxxx 160 C++ */

#include<stdio.h>
#include<string.h>
int main()
{
long int i,j,n,k,p=0;
static int x[500];
memset(x,0,500);
scanf("%ld",&n);
while(n>0)
{ for(i=2;i<=n;i++)
{ j=i;
for(k=2;k<=i;k++)
{ while(j%k==0)
{ ++x[k];
j/=k;
}
}
}
printf("%2ld! = ",n);
for(i=0;i<=99;i++)
{
if(x)
{
p++;
if(p==16&&n==100)
{
printf("\n ");
p=0;
}
if(p==16)
{
printf("\n ");
p=0;
}
printf("%2d ",x);
}
/* if(n>=53&&p>=15)
{
if(n==100)
printf("\n ");
else
printf("\n ");
p=0;
} */
}
memset(x,0,500);
printf("\n");
scanf("%ld",&n);
p=0;
}
return 0;
}

Post Reply

Return to “Volume 1 (100-199)”