Relatives
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8841 | Accepted: 4134 |
Description
Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.
Input
There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.
Output
For each test case there should be single line of output answering the question posed above.
Sample Input
7 12 0
Sample Output
6 4
Source
1 // Project name : 2407 ( Relatives ) 2 // File name : main.cpp 3 // Author : Izumu 4 // Date & Time : Fri Jul 13 16:13:57 2012 5 6 7 #include <iostream> 8 #include <stdio.h> 9 #include <string> 10 #include <cmath> 11 #include <algorithm> 12 using namespace std; 13 14 typedef unsigned long long int longint; 15 16 longint phi(longint num) 17 { 18 longint sum = 1; 19 for (longint i = 2; i <= sqrt(num); i++) 20 { 21 if(num % i == 0) 22 { 23 while (num % i == 0) 24 { 25 sum *= i; 26 num /= i; 27 } 28 sum /= i; 29 sum *= (i - 1); 30 } 31 } 32 33 if (num != 1) 34 { 35 sum *= (num - 1); 36 } 37 38 return sum; 39 } 40 41 int main() 42 { 43 longint number; 44 while (cin >> number && number) 45 { 46 cout << phi(number) << endl; 47 } 48 return 0; 49 } 50 51 // end 52 // ism