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);
}
}
}
留言
張貼留言