T-Prime(数学规律)
2016-04-16 18:50
260 查看
Description
We know that prime numbers are positive integers that have exactly two distinct positive divisors.
Similarly, we’ll call a positive integer t Т-prime, if t has exactly three distinct positive divisors。
You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
Input
The first line contains a single positive integer, n (1 ≤ n ≤ 10000),showing how many numbers are in the array.
The next line contains n space-separated integers xi (1 ≤ xi ≤ 10^12).
Output
Print one line: The number of T-primes number
Sample Input
Copy sample input to clipboard
3
4 5 6
Sample Output
1
Hint
Please use long long instead of int for any Xi.
The given test has three numbers.
The first number 4 has exactly three divisors — 1, 2 and 4.
The second number 5 has two divisors (1 and 5),
The third number 6 has four divisors (1, 2, 3, 6),
hence the answer for them is 1 (only the number 4 is T-primes number).
代码实现
一个数一定可以被 1 和 它本身整除。所以要考虑的是是否存在另外一个数可以整除n。
假如存在这么一个数a,
那么显然(n/a)也可以整除n。
那么也就是说 a = (n / a);
a * a = n;
所以假如 n 是一个T-Prime, 那么 n 必然是完全平方数,并且其平方根必须是素数。
由此,我们可以得到一个判定T-Prime的方法。
We know that prime numbers are positive integers that have exactly two distinct positive divisors.
Similarly, we’ll call a positive integer t Т-prime, if t has exactly three distinct positive divisors。
You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
Input
The first line contains a single positive integer, n (1 ≤ n ≤ 10000),showing how many numbers are in the array.
The next line contains n space-separated integers xi (1 ≤ xi ≤ 10^12).
Output
Print one line: The number of T-primes number
Sample Input
Copy sample input to clipboard
3
4 5 6
Sample Output
1
Hint
Please use long long instead of int for any Xi.
The given test has three numbers.
The first number 4 has exactly three divisors — 1, 2 and 4.
The second number 5 has two divisors (1 and 5),
The third number 6 has four divisors (1, 2, 3, 6),
hence the answer for them is 1 (only the number 4 is T-primes number).
代码实现
#include<iostream> #include<cmath> using namespace std; long long data[10000]; bool isTPrime(long long n) { long long temp = sqrt(n); if(temp * temp == n) { if(temp < 2) return false; else { for(long long i = 2; i <= sqrt(temp); i++){ if(temp % i == 0){ return false; } } return true; } } else return false; } int main() { int n; while(cin >> n){ int result = 0; for(long long i = 0; i < n; i++){ cin >> data[i]; } for(long long i = 0; i < n; i++){ if(isTPrime(data[i])){ result ++; } } cout << result << endl; } return 0; }
一个数一定可以被 1 和 它本身整除。所以要考虑的是是否存在另外一个数可以整除n。
假如存在这么一个数a,
那么显然(n/a)也可以整除n。
那么也就是说 a = (n / a);
a * a = n;
所以假如 n 是一个T-Prime, 那么 n 必然是完全平方数,并且其平方根必须是素数。
由此,我们可以得到一个判定T-Prime的方法。