Sum of all numbers up to N that are co-prime with N - GeeksforGeeks

07 Apr, 2021
Given an integer N, the task is to find the sum of all numbers in the range [1, N] that are co-prime with the given number N.
Examples:
Input: N = 5Output: 10Explanation:Numbers which are co-prime with 5 are {1, 2, 3, 4}.Therefore, the sum is given by 1 + 2 + 3 + 4 = 10.
Input: N = 6Output: 5Explanation:Numbers which are co-prime to 6 are {1, 5}.Therefore, the required sum is equal to 1 + 5 = 6
Approach: The idea is to iterate over the range [1, N], and for every number, check if its GCD with N is equal to 1 or not. If found to be true, for any number, then include that number in the resultant sum. Follow the steps below to solve the problem:
Initialize the sum as 0.
Iterate over the range [1, N] and if GCD of i and N is 1, add i to sum.
After completing the above steps, print the value of the sum obtained.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <iostream>using namespace std; // Function to return gcd of a and bint gcd(int a, int b){    // Base Case    if (a == 0)        return b;     // Recursive GCD    return gcd(b % a, a);} // Function to calculate the sum of all// numbers till N that are coprime with Nint findSum(unsigned int N){    // Stores the resultant sum    unsigned int sum = 0;     // Iterate over [1, N]    for (int i = 1; i < N; i++) {         // If gcd is 1        if (gcd(i, N) == 1) {             // Update sum            sum += i;        }    }     // Return the final sum    return sum;} // Driver Codeint main(){    // Given N    int N = 5;     // Function Call    cout << findSum(N);    return 0;}
// Java program for the above approachimport java.util.*;class GFG{ // Function to return gcd// of a and bstatic int gcd(int a,               int b){  // Base Case  if (a == 0)    return b;   // Recursive GCD  return gcd(b % a, a);} // Function to calculate the// sum of all numbers till N// that are coprime with Nstatic int findSum(int N){  // Stores the resultant sum  int sum = 0;   // Iterate over [1, N]  for (int i = 1; i < N; i++)  {    // If gcd is 1    if (gcd(i, N) == 1)    {      // Update sum      sum += i;    }  }   // Return the final sum  return sum;} // Driver Codepublic static void main(String[] args){  // Given N  int N = 5;   // Function Call  System.out.print(findSum(N));}} // This code is contributed by gauravrajput1
# Python program for# the above approach # Function to return gcd# of a and bdef gcd(a, b):    # Base Case    if (a == 0):        return b;     # Recursive GCD    return gcd(b % a, a); # Function to calculate the# sum of all numbers till N# that are coprime with Ndef findSum(N):    # Stores the resultant sum    sum = 0;     # Iterate over [1, N]    for i in range(1, N):        # If gcd is 1        if (gcd(i, N) == 1):            # Update sum            sum += i;     # Return the final sum    return sum; # Driver Codeif __name__ == '__main__':    # Given N    N = 5;     # Function Call    print(findSum(N)); # This code is contributed by Rajput-Ji
// C# program for the above approachusing System;class GFG{ // Function to return gcd// of a and bstatic int gcd(int a,               int b){  // Base Case  if (a == 0)    return b;   // Recursive GCD  return gcd(b % a, a);} // Function to calculate the// sum of all numbers till N// that are coprime with Nstatic int findSum(int N){  // Stores the resultant sum  int sum = 0;   // Iterate over [1, N]  for (int i = 1; i < N; i++)  {    // If gcd is 1    if (gcd(i, N) == 1)    {      // Update sum      sum += i;    }  }   // Return the readonly sum  return sum;} // Driver Codepublic static void Main(String[] args){  // Given N  int N = 5;   // Function Call  Console.Write(findSum(N));}} // This code is contributed by shikhasingrajput
<script> // Javascript program for the above approach // Function to return gcd of a and bfunction gcd(a, b){    // Base Case    if (a == 0)        return b;     // Recursive GCD    return gcd(b % a, a);} // Function to calculate the sum of all// numbers till N that are coprime with Nfunction findSum(N){    // Stores the resultant sum    var sum = 0;     // Iterate over [1, N]    for (var i = 1; i < N; i++) {         // If gcd is 1        if (gcd(i, N) == 1) {             // Update sum            sum += i;        }    }     // Return the final sum    return sum;} // Driver Code// Given Nvar N = 5; // Function Calldocument.write(findSum(N));  </script>
10
 
Time Complexity: O(N)Auxiliary Space: O(1)
GauravRajput1
shikhasingrajput
Rajput-Ji
rrrtnx
HCF
prime-factor
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects