Search code examples
rubypalindrome

Finding six-digit palindrome number. Works for four and five digits


I have to find palindrome numbers which are Product of three-digit numbers. The program I made works for 2*2 and 3*2. But not for 3*3. Why? I can't figure out.

#initialized variables

x = 999             
y = 999


 while x > 100

    while y > 100

        num = x*y

        #Reversing the digits

        a = num/100000
        b = num%100000
        c = b/10000
        d = b%10000
        e = d/1000
        f = d%1000
        g = f/100
        h = f%100
        i = h/10
        j = h%10
        rev = 100000*j+10000*i+1000*g+100*e+10*c+a

        #Checking for palindrome

        if rev == num
            puts num
        end
        y -= 1

    end
    x -= 1  

end

Solution

  • You're not resetting y to 999 after each iteration of x, so your program isn't actually iterating over the full range of values as intended. Bump y = 999 to just under while x > 100.