📜  与N互质的所有不超过N的数字之和

📅  最后修改于: 2021-05-06 23:19:13             🧑  作者: Mango

给定整数N ,任务是查找与给定数字N互质的[1,N]范围内所有数字的总和。

例子:

方法:想法是在[1,N]范围内进行迭代,对于每个数字,检查其N的GCD是否等于1 。如果发现为真,则对于任何数字,都应将该数字包括在结果总和中。请按照以下步骤解决问题:

  • 总和初始化为0。
  • 迭代[1,N]范围,如果iN的GCD1 ,则将i加到sum上
  • 完成上述步骤后,打印获得的总和的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to return gcd of a and b
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 N
int 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 Code
int main()
{
    // Given N
    int N = 5;
 
    // Function Call
    cout << findSum(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to return gcd
// of a and b
static 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 N
static 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 Code
public static void main(String[] args)
{
  // Given N
  int N = 5;
 
  // Function Call
  System.out.print(findSum(N));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python program for
# the above approach
 
# Function to return gcd
# of a and b
def 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 N
def 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 Code
if __name__ == '__main__':
    # Given N
    N = 5;
 
    # Function Call
    print(findSum(N));
 
# This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to return gcd
// of a and b
static 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 N
static 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 Code
public static void Main(String[] args)
{
  // Given N
  int N = 5;
 
  // Function Call
  Console.Write(findSum(N));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
10

时间复杂度: O(N)
辅助空间: O(1)