Project Euler #36: Double-base palindromes

Sort by

recency

|

25 Discussions

|

  • + 0 comments

    Solution without modulo convertion | Java 100p

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int k = in.nextInt();
            System.out.println(getPalindromsSum(n, k));
        }
        
        private static  long getPalindromsSum(int n, int k) {
            int[] digits10 = new int[100];
            int[] digitsK = new int[100];
            long sum = 1;
            int size10 = 1;
            int sizeK = 1;
            digits10[0] = 1;
            digitsK[0] = 1;
            for (int i=2; i<n; ++i) {
                size10 = incrementDigits(digits10, 10, size10);
                sizeK = incrementDigits(digitsK, k, sizeK);
                if (isPalindrom(digits10, size10) && isPalindrom(digitsK, sizeK))
                    sum += i;
            }
            return sum;
        }
    
        private static int incrementDigits(int[] digits, int k, int sizeDigits) {
            int p = 0;
            while (digits[p] == k-1) {
                digits[p] = 0;
                p++;
            }
            digits[p] = digits[p] + 1;
            return Math.max(p+1, sizeDigits);
        }
        
        private static boolean isPalindrom(int[] digits, int sizeDigits) {
            int max = sizeDigits/2;
            for (int i=0; i<max; ++i) {
                if (digits[i] != digits[sizeDigits-1-i]) {
                    return false;
                }
            }
            return true;
        }
    }
    
  • + 0 comments

    100 points.

    def convert(n,b):
        if n==0:
            return 0
        res=''
        while n>0:
            n,r=divmod(n,b)
            res=str(r)+res
        return res    
    n,k=map(int,input().strip().split())
    s=0
    for i in range(1,n):
        c=convert(i,k)
        if str(i)==str(i)[::-1] and c==c[::-1]:
            s+=i
    print(s)
    
  • + 0 comments

    JAva Code

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        
        static String valid(int n, int k) {
            StringBuilder s = new StringBuilder();
            while (n > 0) {
                int r = n % k;
                n /= k;
                s.append(Integer.toString(r));
            }
            return s.reverse().toString();
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String[] input = scanner.nextLine().split(" ");
            int n = Integer.parseInt(input[0]);
            int k = Integer.parseInt(input[1]);
            int t = 0;
    
            for (int i = 1; i <= n; i++) {
                String strI = Integer.toString(i);
                if (strI.equals(new StringBuilder(strI).reverse().toString())) {
                    String validStr = valid(i, k);
                    if (validStr.equals(new StringBuilder(validStr).reverse().toString())) {
                        t += i;
                    }
                }
            }
    
            System.out.println(t);
            scanner.close();
        }
    }
    
  • + 0 comments

    here is my c# 100 ponits solution

    using System;
    
    class Solution {
        static string Valid(int n, int k) {
            string s = "";
            while (n > 0) {
                int r = n % k;
                n /= k;
                s += r.ToString();
            }
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    
        static void Main(string[] args) {
            string[] input = Console.ReadLine().Split();
            int n = int.Parse(input[0]);
            int k = int.Parse(input[1]);
            int t = 0;
    
            for (int i = 1; i <= n; i++) {
                if (i.ToString() == new string(i.ToString().ToCharArray().Reverse().ToArray())) {
                    if (Valid(i, k) == new string(Valid(i, k).ToCharArray().Reverse().ToArray())) {
                        t += i;
                    }
                }
            }
    
            Console.WriteLine(t);
        }
    }
    
  • + 0 comments

    You just need to know how to convert a number to any base, and rest is python.

    def no_to_base(no,base):
        ans=''
        while no:
            ans=str(no%base)+ans
            no=no//base
        return int(ans)
    
    def ispalindrome(n):
        n=str(n)
        if n==n[::-1]:
            return True
        else:
            return False
    item=input().split()
    n=int(item[0])
    k=int(item[1])
    
    req_sum=0
    for i in range(1,n):
        if ispalindrome(i) and ispalindrome(no_to_base(i,k)):
            req_sum+=i
            
    print(req_sum)