# Thread: Beginner Programming Challenge #10

1. ## Re: Beginner Programming Challenge #10

And again in ruby.

Code:
```DEPTH = 30
MAX_LYCHREL = 10000

class Integer
def is_lychrel?(d=DEPTH)
def reverse
return self.to_s.reverse.to_i
end

if d == 0
return true
elsif (d != DEPTH) and (self == self.reverse)
return false
end

return (self + self.reverse).is_lychrel?(d-1)
end
end

puts (1..MAX_LYCHREL).select { |x| x.is_lychrel? }```

2. First Cup of Ubuntu
Join Date
Jun 2011
Beans
2

## Re: Beginner Programming Challenge #10

I wouldn't consider this a beginner challenge.

3. ## Re: Beginner Programming Challenge #10

Originally Posted by JackDarkStone
I wouldn't consider this a beginner challenge.
Jack:

Perhaps, but it is a fairly simple program once you understand the problem.

4. Just Give Me the Beans!
Join Date
Jun 2011
Beans
77

## Re: Beginner Programming Challenge #10

That was fun and challenging
Code:
```def rev_N(N):							#reverse a number
return int(str(N)[::-1])

def main():
for N in range(11,10001,1):					#Testing numbers 11 to 10000
attempt_N = 0
original_number = N
number = original_number
reverse = rev_N(number)

while attempt_N < 30 and number != reverse:		#try 30 times or until a palindrome has been found
attempt_N = attempt_N + 1 				#counting tries

if number != reverse and attempt_N == 30:		#After attempt_N attempts a palindrome has not been formed
print(original_number,'is a lychrel candidate') #until the 30th iteration

elif number != reverse:				#if the numbers are not equal yet:
number = number + reverse			#add them together to make new number
reverse = rev_N(number)				#and new reverse number
main()```
#I realise it tests some numbers twice like 59 and 95, and then 95 and 59. Not sure if it's easy to fix that.

5. ## Re: Beginner Programming Challenge #10

Thread closed in 3, 2, 1...

6. First Cup of Ubuntu
Join Date
Aug 2011
Beans
10
Distro
Ubuntu 11.04 Natty Narwhal

## Re: Beginner Programming Challenge #10

You can't prove that a number is lychrel number ... You can take n iterations to get to a palindrome and prove it's a non-lychrel number, but if you want to determine it's a lychrel number you'll need to take all the possible iterations, and that's an infinite number of iterations. Sorry man great try !

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•