Page 5 of 6

10042 smith number wa>>>>

Posted: Thu Jul 06, 2006 2:50 pm
by ayeshapakhi
hi..
pls help me finding the bug>>>
thanks for any help...

Code: Select all

#include <stdio.h>   //10042 Submit ###
#include <math.h>

#define U 47001
#define N 5000

bool tag[U];
long primes[N],c=0;

inline bool isprime(long n);
inline void seive(void);

void main()
{
	long num,all[N],i,j,k,intg,real,sumd,sumpd,test,l;	

	seive();

	scanf("%ld",&test);
		
	for(k=0; k<test; k++)
	{
		scanf("%ld",&num);

		for(l=num;   ; l++)
		{
			real=l;
			sumd=0;
			while( real !=0)
			{
				sumd+=real%10;
				real =real/10;
			}

			i=0;
			intg=l;
			for(j=0; ; j++)
			{
				if( intg==1) break;
				if( isprime(intg) )
				{
					all[i++]=intg;
					break;
				}
				while( intg % primes[j] == 0)
				{
					all[i++]=primes[j];
					intg/=primes[j];
				}
			}

			sumpd=0;

			for(j=0; j<i; j++)
			{
				intg=all[j];

				while( intg!=0)
				{
					sumpd+=intg%10;
					intg=intg/10;
				}
			}
			if(sumd== sumpd) 
			{
				printf("%ld\n",l);
				break;
			}
		}
	}
}

inline bool isprime(long n)
{
	long i,temp;
	if(n==1) return false;

	if(n==2) return true;

	if(n%2==0) return false;
	temp=(long)sqrt(n);

	for(i=0; primes[i]<=temp; i++) if(n % primes[i]==0) return false;

	return true;
}

inline void seive(void)
{
	long i,j,r;

	for(i=2; i<U; i++) tag[i]=true;

	primes[c++]=2;

	for(i=3; i<U; i+=2)
	{
		if( tag[i])
		{
			primes[c++]=i;
			
			if( (U/i) >= i)
			{
				r=i*2;
				for(j=i*i; j<U; j+=r) tag[j]=false;
			}
		}
	}
}

thanks.

Posted: Wed Aug 02, 2006 10:32 pm
by 898989
I did not read your code carefully...But her is a good note
Most who get wrong answer assume smith number is a prime number
I think you do that

Note
No need for generating any primes...Just a function that try all i that less than or equal to its sqrt
If you need more about this Just ask

Posted: Thu Aug 24, 2006 9:20 am
by Kallol
I am at my wits end what to do. I tried by all means but could not break the vicious cycle of WA from the online judge !!
here is my code:

Code: Select all

cut after ACC
It passes all the inputs . I checked it with almost possible inputs and everytime it gives me the right answer. I dont know where it is going wrong . Is theke anyone to help me?

Posted: Thu Aug 24, 2006 11:52 pm
by tan_Yui
Hi, Kallol.

http://online-judge.uva.es/board/viewto ... ight=10042
This thread will just help you.

Best regards.

Posted: Fri Aug 25, 2006 9:56 pm
by Kallol
Thanx a lot.
I just missed out the case for 2.
I almost left trying that problem and wasted lots of my time for that silly mistake.Now I got ACCEPTED.
Thank u once again :lol:

Need some I/O

Posted: Wed Oct 04, 2006 9:01 am
by marif
There are some inputs but no corresponding outputs. Pls someone post correct outputs.
Thnx in advance.

WA... somebody plz help me out!!

Posted: Tue May 29, 2007 4:17 pm
by pradeepr
this is the code...



#include<iostream>
#include<cstdlib>
#include<vector>
#include<cmath>
using namespace std;
int sum_digits(long num)
{
int sum=0;
while(num!=0)
{
sum+=num%10;
num/=10;
}
return sum;
}
int main()
{
vector<long> primes;
vector<int>primes_sum;
primes.push_back(2);
primes_sum.push_back(2);
long limit=(long)floor(sqrt(1000000000)),num,j;
int num_cases;
for(long i=3;i<=limit;i++)
{
bool flag=true;
int size=primes.size();
for (int j=0;j<size;j++)
{
if(i%primes[j]==0)
{
flag=false;
break;
}
}

if(flag)
{
primes.push_back(i);
primes_sum.push_back(sum_digits(i));
}

}
cin >>num_cases;
for(int k=0;k<num_cases;k++)
{
std::cin >>j;
j++;
while (1)
{
int total_count=0;
num=j;
int digits=sum_digits(num),prime_digits=0;
for(int i=0;i<primes.size()&&num!=1;i++)
{
int count=0;
while(num%primes==0)
{
count++;
num/=primes;
total_count++;
}
prime_digits+=primes_sum*count;
count=0;
}
if(num!=1)
{
prime_digits+=sum_digits(num);
total_count++;
}
if(digits==prime_digits && total_count>1 )
{
cout<<j<<'\n';
break;

}
else
j++;
}
}
return(0);
}

I tried out evrything....but couldnt find the mistake...
please help me out..

Re: 10042 - Smith Numbers

Posted: Thu May 22, 2008 6:58 pm
by dplt
Anybody who still got WA should read these notes:
1. Prime numbers are NOT Smith numbers
2. Integer data type is ENOUGH, instead using unsigned or long long
3. Output for 1,000,000,000 is 1,000,000,165
4. Output for 1 is 4 !!!

Re: 10042 - Smith Numbers

Posted: Mon Oct 18, 2010 4:04 pm
by Shafaet_du
sample:

Code: Select all

10
123423
345332
456456
23
434535
999
234523
233232
45454
54645
output:

Code: Select all

123464
345343
456502
27
434542
1086
234562
233257
45495
54654

10042 smith number wa>>>>

Posted: Tue Jul 23, 2013 10:03 am
by hwj1593
#include <stdio.h>

unsigned Factorize(unsigned u);
int sumPosNumber(unsigned number);

int main()
{
unsigned number = 29716;
unsigned smith= 0;
unsigned result = 1;
unsigned max;

scanf("%ld",&max);
for(unsigned i=0;i<max;i++){
scanf("%ld",&number);
number++;
while(smith != result )
{
result = Factorize(number);
// printf("result = %ld\n",result);
if(result != 0){
smith = sumPosNumber(number);
// printf("smith = %ld\n",smith);
}
number++;
}
printf("%ld\n",number-1);
result = 0;
}
return 0;
}

unsigned Factorize(unsigned u)
{
unsigned init = u;
unsigned result = 0;
unsigned temp;

for(unsigned i = 2;i<u;)
{
if(u%i == 0)
{
temp = sumPosNumber(i);
//printf("%ld",temp);
result += temp;
if (u!=i)
{
// printf("*");
u /= i;
i = 2;
}
}else
i++;

}
if(init == u){

return 0;
}
// printf("%d\n",u);


temp = sumPosNumber(u);
return result+temp;
}

int sumPosNumber(unsigned number)
{
int smith = 0;
int temp;

while(1)
{
if(number == 0){
break;
}else{
temp = number%10;
smith += temp;
}

number = number/10;
}
return smith;
}

why ... wa?
i'm not good well english... sorry about ..

Re: 10042 smith number wa>>>>

Posted: Wed Jul 24, 2013 3:25 am
by brianfry713
scanf and printf should use %u for unsigned, %ld is for a long.

Re: 10042 smith number wa>>>>

Posted: Thu Jul 25, 2013 9:36 am
by hwj1593
thanks to brianfry713 !! time limit t. t

Re: 10042 smith number wa>>>>

Posted: Sat Jul 27, 2013 4:04 am
by brianfry713
Start by precomputing a list of primes.

Re: 10042 - Smith Numbers

Posted: Wed Oct 30, 2013 5:27 pm
by mahade hasan
Acc

Re: 10042 - Smith Numbers

Posted: Thu Oct 31, 2013 10:20 pm
by brianfry713
Try to speed up your Cal function. If you only want 10,000 primes you only need to check up to 104,729, not 100,000,000.