# Euler’s Totient Function

Posted: 26 Oct, 2020
Difficulty: Easy

## PROBLEM STATEMENT

#### You are given an integer 'N'. Your task is to count the number of integers between 1 and 'N' both inclusive which are coprime to 'N'.

##### Note:
``````Two numbers are coprime if their greatest common divisor(GCD) is 1.

Here, 1 is considered to be coprime to any number.
``````
##### For Example:
``````If the given integer is 9, then the answer would be 6 Because there are six numbers between 1 and 9 both inclusive which are coprime to 9 i.e 1, 2, 4, 5, 7, and 8.
``````
##### Input Format:
``````The first line of input contains an integer 'T' representing the number of test cases or queries to be processed.

Then the test case follows.

The only line of each test case contains an integer 'N'.
``````
##### Output Format:
``````For each test case, print a single line containing a single integer denoting the total numbers of integers between 1 and 'N'(both inclusive) which are coprime to 'N', in a single line.

The output of each test case will be printed in a separate line.
``````
##### Note:
``````You don't have to print anything. It has already been taken care of. Just implement the function.
``````
##### Constraints:
``````1 <= T <= 100
1 <= N <= 10 ^ 9

Time Limit: 1 sec.
`````` Approach 1

The better solution is to use Euler’s product formula which states that the total number of integers between 1 and N - 1 inclusive which are coprime to N can be found using this formula Euler Product Formula N * (1 - (1 / p1)) * (1 - (1 / p2)) *.......* (1 - (1 / pk)) where p1,p2...pk are the prime factors of N.

So now the problem is to find all the prime factors and use the above formula.

To find the prime factors and count the number of integers between 1 and N which are coprime to N below are the steps.

Steps:

1. Initially declare a variable total = N.
2. Then start a loop from p = 2 to sqrt(N) and do the following:
1. if(N % p == 0) then
1. While p divides N:
1. Divide N by p.
2. total = total - total / p
3. If N is a prime number and is greater than 1, then:
1. total = total - total / N
4. At last, return the total.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9032914,"math_prob":0.98743564,"size":2021,"snap":"2022-05-2022-21","text_gpt3_token_len":557,"char_repetition_ratio":0.1363411,"word_repetition_ratio":0.098522164,"special_character_ratio":0.29638794,"punctuation_ratio":0.13181818,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9996643,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-22T13:51:07Z\",\"WARC-Record-ID\":\"<urn:uuid:eed633f0-e0d6-46d2-bdd2-d77d44050067>\",\"Content-Length\":\"126243\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:96f8ef4b-aa01-425b-90ae-978fc57883f8>\",\"WARC-Concurrent-To\":\"<urn:uuid:bc3d4800-a88c-4518-bbb2-2b9117d5349a>\",\"WARC-IP-Address\":\"18.67.65.75\",\"WARC-Target-URI\":\"https://www.codingninjas.com/codestudio/problem-details/total-number-coprime-to-n_800302\",\"WARC-Payload-Digest\":\"sha1:W3NWJQWTVUWJMIM7QS6YTWKIZJ7IYMII\",\"WARC-Block-Digest\":\"sha1:E4UJTA4U5SDWJGLQT2GWVXYC5XUBG53D\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662545548.56_warc_CC-MAIN-20220522125835-20220522155835-00112.warc.gz\"}"}