Project Euler #36: Double-base palindromes

  • + 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);
        }
    }