Page 10 of 10 FirstFirst ... 8910
Results 91 to 96 of 96

Thread: Beginner Programming Challenge #10

  1. #91
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    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? }
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  2. #92
    Join Date
    Jun 2011
    Beans
    2

    Re: Beginner Programming Challenge #10

    I wouldn't consider this a beginner challenge.

  3. #93
    cprofitt's Avatar
    cprofitt is offline νόησις νοήσεως - nóesis noéseos
    Join Date
    Oct 2006
    Location
    平静
    Beans
    1,451
    Distro
    Ubuntu Development Release

    Re: Beginner Programming Challenge #10

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

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

  4. #94
    Join Date
    Jun 2011
    Beans
    80

    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. #95
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Beginner Programming Challenge #10

    Thread closed in 3, 2, 1...
    「明後日の夕方には帰ってるからね。」


  6. #96
    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 !

Page 10 of 10 FirstFirst ... 8910

Tags for this Thread

Bookmarks

Posting Permissions

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