Project Euler #35: Circular primes

  • + 0 comments

    here is my c# 100 point answer

    using System;
    
    class Solution {
        static bool IsPrime(int x) {
            for (int i = 2; i * i <= x; i++) {
                if (x % i == 0) {
                    return false;
                }
            }
            return true;
        }
    
        static bool AreAllRotationsPrime(int x) {
            int d = 1;
            int num = x;
            int digits = 0;
            
            while (d <= num) {
                d *= 10;
                digits++;
            }
            d /= 10;
    
            for (int k = 0; k < digits; k++) {
                int rotation = (x % 10) * d + (x / 10);
                if (!IsPrime(rotation)) {
                    return false;
                }
                x = rotation;
            }
    
            return true;
        }
    
        static void Main(string[] args) {
            int n = int.Parse(Console.ReadLine());
            int sum = 0;
    
            for (int i = 2; i < n; i++) {
                if (IsPrime(i) && AreAllRotationsPrime(i)) {
                    sum += i;
                }
            }
    
            Console.WriteLine(sum);
        }
    }