## 640 - Self Numbers

Moderator: Board moderators

henar2
New poster
Posts: 30
Joined: Mon Nov 26, 2001 2:00 am

### 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?
agutierrez1@usuarios.retecal.es

cyfra
Experienced poster
Posts: 144
Joined: Thu Nov 22, 2001 2:00 am
Location: Gdynia, Poland
Hi

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>

angga888
Experienced poster
Posts: 143
Joined: Sat Dec 21, 2002 11:41 am
Location: Indonesia

### 640 Self Number WA

Help me, I'm getting WA on this problem.
How many self numbers are there below 10000 and below 50000?

cytse
Learning poster
Posts: 67
Joined: Mon Sep 16, 2002 2:47 pm
Location: Hong Kong
Contact:
<10000: 983
<50000: 4895

akhter900
New poster
Posts: 7
Joined: Fri Mar 19, 2004 3:27 pm

### 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;
}
==============================
AkHtEr

shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA
Hi, consider this segment of your code.

[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.

koodeGuru
New poster
Posts: 23
Joined: Mon Apr 19, 2004 6:24 am

### Volume 6 Problem640

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.

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

### Re: Volume 6 Problem640

koodeGuru 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``
Ciao!!!

Claudio

koodeGuru
New poster
Posts: 23
Joined: Mon Apr 19, 2004 6:24 am

### I knew it

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.

koodeGuru
New poster
Posts: 23
Joined: Mon Apr 19, 2004 6:24 am

### I knew it

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.

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

### Re: I knew it

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.
Basically self(n+1)=self(n)+11.
You only need some extra care when you step over multiples of 10^n with n>=2.

Ciao!!!

Claudio

Pavl0
New poster
Posts: 16
Joined: Sun Apr 18, 2004 2:57 pm
U can also check all number if it is self number
J get AC by deleting numbers who are not self number's

Arm.Turbo
New poster
Posts: 21
Joined: Wed Aug 11, 2004 1:20 pm
Here is my code I don't understand why it's WA. Help me pls.

[cpp]Spoiler[/cpp]
Last edited by Arm.Turbo on Fri Aug 27, 2004 12:17 pm, edited 1 time in total.

Ryan Pai
Learning poster
Posts: 67
Joined: Fri Jul 04, 2003 9:59 am
Location: USA
Contact:
Arm.Turbo,
You are only adding five digits, whereas the numbers can be up to six digits.
I'm always willing to help, if you do the same.

Arm.Turbo
New poster
Posts: 21
Joined: Wed Aug 11, 2004 1:20 pm
999943
999945
999956
999967
999978
999989

My last numbers are they correct?