## 640 - Self Numbers

**Moderator:** Board moderators

### 640 - Self Numbers

Hi.

I need help with 640 problem. I wrong answered and I don't understand why.

Could someone send me the solution?

Thank you in advance.

agutierrez1@usuarios.retecal.es

I need help with 640 problem. I wrong answered and I don't understand why.

Could someone send me the solution?

Thank you in advance.

agutierrez1@usuarios.retecal.es

Try do this in brutal way (check all the posibilities)

You can do this in this way:

for x=1 to 1000000 do

if (x was not used before) then

begin

Write (x)

end;

generate new number and check it as used

(you can use an array)

end;

This program should generate all the numbers.

<font size=-1>[ This Message was edited by: cyfra on 2001-12-20 12:56 ]</font>

### 640 Self Number WA

Help me, I'm getting WA on this problem.

How many self numbers are there below 10000 and below 50000?

Please someone answer my question and Thanks.

How many self numbers are there below 10000 and below 50000?

Please someone answer my question and Thanks.

### 640 _ Runtime error, help PLS.

My code is bellow. It generates Runtime Error. Would U help me?

==============================

#include<stdio.h>

#include<bitset>

#include<string>

#include<iostream>

using namespace std;

bitset <1000010> self_num;

long long gnrt(long long a)

{

unsigned long j = a;

while(a != 0)

{

j += a%10; a /= 10;

}

return j;

}

int main()

{

long long i, j;

for(i = 1; i <= 1000000; i++)

if(self_num.test(i) == 0)

{

printf("%ld\n", i);

j = gnrt(i) ;

self_num.set(j);

}

else

{

j=gnrt(i); self_num.set(j);

}

return 0;

}

==============================

==============================

#include<stdio.h>

#include<bitset>

#include<string>

#include<iostream>

using namespace std;

bitset <1000010> self_num;

long long gnrt(long long a)

{

unsigned long j = a;

while(a != 0)

{

j += a%10; a /= 10;

}

return j;

}

int main()

{

long long i, j;

for(i = 1; i <= 1000000; i++)

if(self_num.test(i) == 0)

{

printf("%ld\n", i);

j = gnrt(i) ;

self_num.set(j);

}

else

{

j=gnrt(i); self_num.set(j);

}

return 0;

}

==============================

AkHtEr

[cpp]

for(i = 1; i <= 1000000; i++)

if(self_num.test(i) == 0)

{

printf("%ld\n", i);

j = gnrt(i) ;

self_num.set(j);

}

else

{

j=gnrt(i); self_num.set(j);

}

[/cpp]

when i is very large, j will be a value which is larger than the size of your bitset, hence you will get RTE when attempting to set that bit.

### Volume 6 Problem640

In generating self numbers of base 10 I am using the recursive relation:

My program prints 9 and then directly jumps to 97. What might be wrong with my program? Please reply.

Code: Select all

`g(n)=8*10^(n-1)+g(n-1)+8;`

**Initial condition is g(1)=9**My program prints 9 and then directly jumps to 97. What might be wrong with my program? Please reply.

### Re: Volume 6 Problem640

your program is rightkoodeGuru wrote:In generating self numbers of base 10 I am using the recursive relation:

Code: Select all

`g(n)=8*10^(n-1)+g(n-1)+8;`

Initial condition is g(1)=9

My program prints 9 and then directly jumps to 97. What might be wrong with my program? Please reply.

Code: Select all

`g(2)=8*10^(2-1)+g(1)+8=8*10+9+8=97`

Claudio

### Re: I knew it

Basically self(n+1)=self(n)+11.koodeGuru wrote:I knew that I was getting appropriate output. But how do I print out all self numbers less than 1000000. Do I have to use a different recursive relation?Thanks.

You only need some extra care when you step over multiples of 10^n with n>=2.

Ciao!!!

Claudio