Implementación del número Lychrel

El número de Lychrel es un número natural que no puede formar un palíndromo a través del proceso iterativo de invertir repetidamente sus dígitos y sumar los números resultantes. El proceso a veces se denomina algoritmo 196 , por el número más famoso asociado con el proceso. 
Los primeros números que no se sabe que producen palíndromos cuando se aplica el algoritmo 196 (es decir, una secuencia inversa y luego suma) a veces se conocen como números de Lychrel. 
Ejemplos: 
 

Input : 56
Output : 56 is lychrel  : false
Explanation : 56 becomes palindromic after one iteration : 
56 + 65 = 121

Input : 196
Output : 196 is lychrel  : true
Explanation : 196 becomes palindromic after 19 iterations :
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
....
16403234045 + 54043230461
70446464506 + 60546464407

La tarea es encontrar si un número dado es Lycheral con un límite dado en el número de iteraciones.
 

1. Iterate given number of times
    1. Add number to it's reverse
    2. If 
         the newly formed number is palindrome
       then
          return false  // Number is not lychrel.
2. return true         // Number is lychrel

C++

// C++ Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
#include<iostream>
using namespace std;
 
long reverse(long);
bool isPalindrome(long);
 
// Max Iterations
static int MAX_ITERATIONS = 20;
 
// Function to check whether number is
// Lychrel Number
string isLychrel(long number)
{
    for (int i = 0; i < MAX_ITERATIONS; i++)
    {
        number = number + reverse(number);
         
        if (isPalindrome(number))
            return "false";
    }
     
    return "true";
}
 
// Function to check whether the number is
// Palindrome
bool isPalindrome(long number)
{
    return number == reverse(number);
}
 
// Function to reverse the number
long reverse(long number)
{
    long reverse = 0;
    while (number > 0)
    {
        long remainder = number % 10;
        reverse = (reverse * 10) + remainder;
        number = number / 10;
    }
     
    return reverse;
}
 
// Driver program
int main()
{
    long number = 295;
    cout<<number << " is lychrel? "
                       << isLychrel(number);
}
 
// This code is contributed by Smitha

Java

// Java Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
import java.io.*;
 
public class LychrelNumberTest
{
    // Max Iterations
    private static int MAX_ITERATIONS = 20;
 
    // Function to check whether number is Lychrel Number
    private static boolean isLychrel(long number)
    {
        for (int i = 0; i < MAX_ITERATIONS; i++)
        {
            number = number + reverse(number);
            if (isPalindrome(number))
                return false;
 
        }
        return true;
    }
 
    // Function to check whether the number is Palindrome
    private static boolean isPalindrome(final long number)
    {
        return number == reverse(number);
    }
 
    // Function to reverse the number
    private static long reverse(long number)
    {
        long reverse = 0;
 
        while (number > 0)
        {
            long remainder = number % 10;
            reverse = (reverse * 10) + remainder;
            number = number / 10;
        }
        return reverse;
    }
 
    // driver program
    public static void main(String[] args)
    {
        long number = 295;
        System.out.println(number + " is lychrel? "
                           + isLychrel(number));
    }
}

Python3

# Python3 Program to check whether the given number
# is Lychrel Number or not with given limit
# on number of iterations.
 
# Max Iterations
MAX_ITERATIONS = 20;
 
# Function to check whether number is
# Lychrel Number
def isLychrel(number):
     
    for i in range(MAX_ITERATIONS):
        number = number + reverse(number);
         
        if (isPalindrome(number)):
            return "false";
     
    return "true";
 
# Function to check whether the number
# is Palindrome
def isPalindrome(number):
  
    return number == reverse(number);
 
# Function to reverse the number
def reverse(number):
  
    reverse = 0;
    while (number > 0):
      
        remainder = number % 10;
        reverse = (reverse * 10) + remainder;
        number = int(number / 10);
     
    return reverse;
 
# Driver Code
number = 295;
print(number," is lychrel? ",isLychrel(number));
 
# This code is contributed by mits

C#

// C# Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
using System;
 
class GFG
{
    // Max Iterations
    private static int MAX_ITERATIONS = 20;
 
    // Function to check whether number is Lychrel Number
    private static bool isLychrel(long number)
    {
        for (int i = 0; i < MAX_ITERATIONS; i++)
        {
            number = number + reverse(number);
            if (isPalindrome(number))
                return false;
 
        }
        return true;
    }
 
    // Function to check whether the number is Palindrome
    private static bool isPalindrome( long number)
    {
        return number == reverse(number);
    }
 
    // Function to reverse the number
    private static long reverse(long number)
    {
        long reverse = 0;
 
        while (number > 0)
        {
            long remainder = number % 10;
            reverse = (reverse * 10) + remainder;
            number = number / 10;
        }
        return reverse;
    }
 
    // Driver program
    public static void Main()
    {
        long number = 295;
        Console.Write(number + " is lychrel? "
                        + isLychrel(number));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
 
// Max Iterations
$MAX_ITERATIONS = 20;
 
// Function to check whether number is
// Lychrel Number
function isLychrel($number)
{
    global $MAX_ITERATIONS;
    for ($i = 0; $i < $MAX_ITERATIONS; $i++)
    {
        $number = $number + reverse($number);
         
        if (isPalindrome($number))
            return "false";
    }
     
    return "true";
}
 
// Function to check whether the number
// is Palindrome
function isPalindrome($number)
{
    return $number == reverse($number);
}
 
// Function to reverse the number
function reverse($number)
{
    $reverse = 0;
    while ($number > 0)
    {
        $remainder = $number % 10;
        $reverse = ($reverse * 10) + $remainder;
        $number = (int)($number / 10);
    }
     
    return $reverse;
}
 
// Driver Code
$number = 295;
echo $number . " is lychrel? " .
             isLychrel($number);
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript Program to check
// whether the given number is Lychrel
// Number or not with given limit
// on number of iterations.
 
// Max Iterations
var MAX_ITERATIONS = 20;
 
// Function to check whether
// number is Lychrel Number
function isLychrel(number)
{
    for (var i = 0; i < MAX_ITERATIONS; i++)
    {
        number = number + reverse(number);
        if (isPalindrome(number))
            return false;
 
    }
    return true;
}
 
// Function to check whether the
// number is Palindrome
function isPalindrome(number)
{
    return number == reverse(number);
}
 
// Function to reverse the number
function reverse(number)
{
    var reverse = 0;
 
    while (number > 0)
    {
        var remainder = number % 10;
        reverse = (reverse * 10) + remainder;
        number = parseInt(number / 10);
    }
    return reverse;
}
 
// driver program
var number = 295;
document.write(number + " is lychrel ? "
                + isLychrel(number));
 
 
// This code contributed by shikhasingrajput
 
</script>
Producción: 

295 is lychrel ? true

 

Este artículo es una contribución de Pramod Kumar . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a contribuido@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *