Project Euler #55: Lychrel numbers

Sort by

recency

|

25 Discussions

|

  • + 0 comments

    Java 8

    import java.io.; import java.util.;

    public class Solution {

    public static boolean isPalindrom(long a){
        long num=a,res=0;
        while (num!=0)
        {
            res= res*10+num%10; 
            num /=10;
        }     
        return (res==a)?true:false;
    }
    
    public static long reverse(long a){
        long res=0;
        while (a!=0)
        {
            res= res*10+a%10; 
            a /=10;
        }     
        return res;
    }
    
    
    public static long LychrelNumber(long data){
        int count =0;
        long res=data;
        while(count<60)
        {
            if(isPalindrom(res)) return res;
            res= res + reverse(res);
            count++;           
        }
    
        return -1;
    }
    
    public static void main(String[] args) {
    
        Scanner s = new Scanner(System.in);
        int data = s.nextInt();
    
        HashMap<Long, Integer> map =new HashMap<>();
        int maxCount=0;
        long maxLychrel=0;
    
        for(int i=1; i<=data;i++)
        {
            long temp= LychrelNumber(i);
            if(temp != -1)
            {
                 map.put(temp, map.getOrDefault(temp, 0) + 1);
    
                if(map.get(temp) > maxCount) 
                {maxCount = map.get(temp);
                 maxLychrel= temp;}
            }
    
        }
    
        System.out.println(maxLychrel + " " + maxCount); 
    }
    

    }

  • + 0 comments

    100 points. Python 3

    non_lychrel={}
    n=int(input().strip())
    for i in range(10,n):
        j=0
        t=i
        while j<60 and str(t)!=str(t)[::-1]:
            t=t+int(str(t)[::-1])
            j+=1
        if str(t)==str(t)[::-1]:
            non_lychrel[t]=non_lychrel[t]+1 if t in non_lychrel else 1
    m=max(non_lychrel,key=non_lychrel.get)
    print(m,non_lychrel[m])
    
  • + 0 comments

    100points/- python3

    end_palindromes=[]
    def ispalindrome(x):
        x=str(x)
        return x==x[::-1]
    n=int(input())
    for i in range(1,n+1):
        iteration=0
        while iteration<60:
            if ispalindrome(i):
                end_palindromes.append(i)
                break
            i+=int(str(i)[::-1])
            iteration+=1
    
    max_count=0
    for i in sorted(set(end_palindromes)):
        y=end_palindromes.count(i)
        if y>max_count:
            answer=i
            max_count=y
        
    print(answer,max_count)
    
  • + 0 comments

    Only 1 test case is getting passed even after using dictionary. Why?

  • + 0 comments

    HINT: For Python you should use dictionary instead of list to store values because it saves a lot of running time and space!