C# check prime

using System;
using System.Collections.Generic;
using System.IO;
class Solution {

     static bool isPrime(int n) {
        for(int i=2;i<=Math.Sqrt(n);i++) {
            if(n%i==0) {
                return false;
            }
        }
        return true;
    }

    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int n = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < n; i++) {
            int x = Convert.ToInt32(Console.ReadLine());
            String s;
            if (x >= 2 && isPrime(x)) {
                s = "Prime";
            } else {
                s = "Not prime";
            } 
            Console.WriteLine(s);
        }
    }
            
    
}



Compiler Message
Success
Input (stdin)
Download
  • 3
  • 12
  • 5
  • 7
Expected Output
Download
  • Not prime
  • Prime
  • Prime

留言

這個網誌中的熱門文章

香港袐密行動

要老是忘記, 我更記不起