📌  相关文章
📜  计算1到N范围内的数字,这些数字可以被X整除,但不能被Y整除

📅  最后修改于: 2021-04-22 08:30:04             🧑  作者: Mango

给定两个正整数X和Y,任务是计算1到N范围内的总数,这些总数可以被X整除,但不能被Y整除。

例子:

一个简单的解决方案是计算可被X整除而不是Y的数字,即循环1到N,然后计算可被X整除但不能Y整除的数字。

方法

  1. 对于范围从1到N的每个数字,如果该数字可被X整除而不被Y整除,则递增计数。
  2. 打印计数。

      下面是上述方法的实现:

      C++
      // C++ implementation of above approach
      #include 
      using namespace std;
        
      // Function to count total numbers divisible by
      // x but not y in range 1 to N
      int countNumbers(int X, int Y, int N)
      {
          int count = 0;
          for (int i = 1; i <= N; i++) {
              // Check if Number is divisible
              // by x but not Y
              // if yes, Increment count
              if ((i % X == 0) && (i % Y != 0))
                  count++;
          }
          return count;
      }
        
      // Driver Code
      int main()
      {
        
          int X = 2, Y = 3, N = 10;
          cout << countNumbers(X, Y, N);
          return 0;
      }


      Java
      // Java implementation of above approach
        
      class GFG {
        
          // Function to count total numbers divisible by
          // x but not y in range 1 to N
          static int countNumbers(int X, int Y, int N)
          {
              int count = 0;
              for (int i = 1; i <= N; i++) {
                  // Check if Number is divisible
                  // by x but not Y
                  // if yes, Increment count
                  if ((i % X == 0) && (i % Y != 0))
                      count++;
              }
              return count;
          }
        
          // Driver Code
          public static void main(String[] args)
          {
        
              int X = 2, Y = 3, N = 10;
              System.out.println(countNumbers(X, Y, N));
          }
      }


      Python3
      # Python3 implementation of above approach 
        
      # Function to count total numbers divisible 
      # by x but not y in range 1 to N 
      def countNumbers(X, Y, N): 
        
          count = 0; 
          for i in range(1, N + 1):
                
              # Check if Number is divisible 
              # by x but not Y 
              # if yes, Increment count 
              if ((i % X == 0) and (i % Y != 0)): 
                  count += 1; 
        
          return count; 
        
      # Driver Code 
      X = 2;
      Y = 3;
      N = 10; 
      print(countNumbers(X, Y, N)); 
            
      # This code is contributed by mits


      C#
      // C# implementation of the above approach
      using System;
      class GFG {
        
          // Function to count total numbers divisible by
          // x but not y in range 1 to N
          static int countNumbers(int X, int Y, int N)
          {
              int count = 0;
              for (int i = 1; i <= N; i++) {
                  // Check if Number is divisible
                  // by x but not Y
                  // if yes, Increment count
                  if ((i % X == 0) && (i % Y != 0))
                      count++;
              }
              return count;
          }
        
          // Driver Code
          public static void Main()
          {
        
              int X = 2, Y = 3, N = 10;
              Console.WriteLine(countNumbers(X, Y, N));
          }
      }


      PHP


      C++
      // C++ implementation of above approach
      #include 
      using namespace std;
        
      // Function to count total numbers divisible by
      // x but not y in range 1 to N
      int countNumbers(int X, int Y, int N)
      {
        
          // Count total number divisible by X
          int divisibleByX = N / X;
        
          // Count total number divisible by Y
          int divisibleByY = N / Y;
        
          // Count total number divisible by either X or Y
          int LCM = (X * Y) / __gcd(X, Y);
          int divisibleByLCM = N / LCM;
          int divisibleByXorY = divisibleByX + divisibleByY 
                                           - divisibleByLCM;
        
          // Count total numbers divisible by X but not Y
          int divisibleByXnotY = divisibleByXorY 
                                             - divisibleByY;
        
          return divisibleByXnotY;
      }
        
      // Driver Code
      int main()
      {
        
          int X = 2, Y = 3, N = 10;
          cout << countNumbers(X, Y, N);
          return 0;
      }


      Java
      // Java implementation of above approach
        
      class GFG {
        
          // Function to calculate GCD
        
          static int gcd(int a, int b)
          {
              if (b == 0)
                  return a;
              return gcd(b, a % b);
          }
        
          // Function to count total numbers divisible by
          // x but not y in range 1 to N
        
          static int countNumbers(int X, int Y, int N)
          {
        
              // Count total number divisible by X
              int divisibleByX = N / X;
        
              // Count total number divisible by Y
              int divisibleByY = N / Y;
        
              // Count total number divisible by either X or Y
              int LCM = (X * Y) / gcd(X, Y);
              int divisibleByLCM = N / LCM;
              int divisibleByXorY = divisibleByX + divisibleByY
                                    - divisibleByLCM;
        
              // Count total number divisible by X but not Y
              int divisibleByXnotY = divisibleByXorY 
                                                - divisibleByY;
        
              return divisibleByXnotY;
          }
        
          // Driver Code
          public static void main(String[] args)
          {
        
              int X = 2, Y = 3, N = 10;
              System.out.println(countNumbers(X, Y, N));
          }
      }


      Python3
      # Python 3 implementation of above approach
      from math import gcd
        
      # Function to count total numbers divisible 
      # by x but not y in range 1 to N
      def countNumbers(X, Y, N):
            
          # Count total number divisible by X
          divisibleByX = int(N / X)
        
          # Count total number divisible by Y
          divisibleByY = int(N / Y)
        
          # Count total number divisible 
          # by either X or Y
          LCM = int((X * Y) / gcd(X, Y))
          divisibleByLCM = int(N / LCM)
          divisibleByXorY = (divisibleByX + 
                             divisibleByY - 
                             divisibleByLCM)
        
          # Count total numbers divisible by 
          # X but not Y
          divisibleByXnotY = (divisibleByXorY - 
                              divisibleByY)
        
          return divisibleByXnotY
        
      # Driver Code
      if __name__ == '__main__':
          X = 2
          Y = 3
          N = 10
          print(countNumbers(X, Y, N))
        
      # This code is contributed by
      # Surendra_Gangwar


      C#
      // C# implementation of above approach
        
      using System;
      class GFG {
        
          // Function to calculate GCD
          static int gcd(int a, int b)
          {
              if (b == 0)
                  return a;
              return gcd(b, a % b);
          }
        
          // Function to count total numbers divisible by
          // x but not y in range 1 to N
          static int countNumbers(int X, int Y, int N)
          {
        
              // Count total number divisible by X
              int divisibleByX = N / X;
        
              // Count total number divisible by Y
              int divisibleByY = N / Y;
        
              // Count total number divisible by either X or Y
              int LCM = (X * Y) / gcd(X, Y);
              int divisibleByLCM = N / LCM;
              int divisibleByXorY = divisibleByX + divisibleByY 
                                              - divisibleByLCM;
        
              // Count total number divisible by X but not Y
              int divisibleByXnotY = divisibleByXorY 
                                                - divisibleByY;
        
              return divisibleByXnotY;
          }
        
          // Driver Code
          public static void Main()
          {
        
              int X = 2, Y = 3, N = 10;
              Console.WriteLine(countNumbers(X, Y, N));
          }
      }


      PHP
       $b) 
              return __gcd( $a - $b , $b ); 
        
          return __gcd( $a , $b - $a ); 
      } 
        
      // Function to count total numbers divisible 
      // by x but not y in range 1 to N
      function countNumbers($X, $Y, $N)
      {
        
          // Count total number divisible by X
          $divisibleByX = $N / $X;
        
          // Count total number divisible by Y
          $divisibleByY = $N /$Y;
        
          // Count total number divisible by either X or Y
          $LCM = ($X * $Y) / __gcd($X, $Y);
          $divisibleByLCM = $N / $LCM;
          $divisibleByXorY = $divisibleByX + $divisibleByY - 
                                             $divisibleByLCM;
        
          // Count total numbers divisible by X but not Y
          $divisibleByXnotY = $divisibleByXorY - 
                              $divisibleByY;
        
          return ceil($divisibleByXnotY);
      }
        
      // Driver Code
      $X = 2;
      $Y = 3;
      $N = 10;
      echo countNumbers($X, $Y, $N);
        
      // This is code contrubted by inder_verma
      ?>


      输出:
      4
      

      时间复杂度:O(N)

      高效的解决方案:

      1. 在1到N的范围内,找到可被X整除的总数和可被Y整除的总数
      2. 此外,查找可被X或Y整除的总数
      3. 计算可被X整除但不能被Y整除的总数
        (可被X或Y整除的总数)–(可被Y整除的总数)

      下面是上述方法的实现:

      C++

      // C++ implementation of above approach
      #include 
      using namespace std;
        
      // Function to count total numbers divisible by
      // x but not y in range 1 to N
      int countNumbers(int X, int Y, int N)
      {
        
          // Count total number divisible by X
          int divisibleByX = N / X;
        
          // Count total number divisible by Y
          int divisibleByY = N / Y;
        
          // Count total number divisible by either X or Y
          int LCM = (X * Y) / __gcd(X, Y);
          int divisibleByLCM = N / LCM;
          int divisibleByXorY = divisibleByX + divisibleByY 
                                           - divisibleByLCM;
        
          // Count total numbers divisible by X but not Y
          int divisibleByXnotY = divisibleByXorY 
                                             - divisibleByY;
        
          return divisibleByXnotY;
      }
        
      // Driver Code
      int main()
      {
        
          int X = 2, Y = 3, N = 10;
          cout << countNumbers(X, Y, N);
          return 0;
      }
      

      Java

      // Java implementation of above approach
        
      class GFG {
        
          // Function to calculate GCD
        
          static int gcd(int a, int b)
          {
              if (b == 0)
                  return a;
              return gcd(b, a % b);
          }
        
          // Function to count total numbers divisible by
          // x but not y in range 1 to N
        
          static int countNumbers(int X, int Y, int N)
          {
        
              // Count total number divisible by X
              int divisibleByX = N / X;
        
              // Count total number divisible by Y
              int divisibleByY = N / Y;
        
              // Count total number divisible by either X or Y
              int LCM = (X * Y) / gcd(X, Y);
              int divisibleByLCM = N / LCM;
              int divisibleByXorY = divisibleByX + divisibleByY
                                    - divisibleByLCM;
        
              // Count total number divisible by X but not Y
              int divisibleByXnotY = divisibleByXorY 
                                                - divisibleByY;
        
              return divisibleByXnotY;
          }
        
          // Driver Code
          public static void main(String[] args)
          {
        
              int X = 2, Y = 3, N = 10;
              System.out.println(countNumbers(X, Y, N));
          }
      }
      

      Python3

      # Python 3 implementation of above approach
      from math import gcd
        
      # Function to count total numbers divisible 
      # by x but not y in range 1 to N
      def countNumbers(X, Y, N):
            
          # Count total number divisible by X
          divisibleByX = int(N / X)
        
          # Count total number divisible by Y
          divisibleByY = int(N / Y)
        
          # Count total number divisible 
          # by either X or Y
          LCM = int((X * Y) / gcd(X, Y))
          divisibleByLCM = int(N / LCM)
          divisibleByXorY = (divisibleByX + 
                             divisibleByY - 
                             divisibleByLCM)
        
          # Count total numbers divisible by 
          # X but not Y
          divisibleByXnotY = (divisibleByXorY - 
                              divisibleByY)
        
          return divisibleByXnotY
        
      # Driver Code
      if __name__ == '__main__':
          X = 2
          Y = 3
          N = 10
          print(countNumbers(X, Y, N))
        
      # This code is contributed by
      # Surendra_Gangwar
      

      C#

      // C# implementation of above approach
        
      using System;
      class GFG {
        
          // Function to calculate GCD
          static int gcd(int a, int b)
          {
              if (b == 0)
                  return a;
              return gcd(b, a % b);
          }
        
          // Function to count total numbers divisible by
          // x but not y in range 1 to N
          static int countNumbers(int X, int Y, int N)
          {
        
              // Count total number divisible by X
              int divisibleByX = N / X;
        
              // Count total number divisible by Y
              int divisibleByY = N / Y;
        
              // Count total number divisible by either X or Y
              int LCM = (X * Y) / gcd(X, Y);
              int divisibleByLCM = N / LCM;
              int divisibleByXorY = divisibleByX + divisibleByY 
                                              - divisibleByLCM;
        
              // Count total number divisible by X but not Y
              int divisibleByXnotY = divisibleByXorY 
                                                - divisibleByY;
        
              return divisibleByXnotY;
          }
        
          // Driver Code
          public static void Main()
          {
        
              int X = 2, Y = 3, N = 10;
              Console.WriteLine(countNumbers(X, Y, N));
          }
      }
      

      的PHP

       $b) 
              return __gcd( $a - $b , $b ); 
        
          return __gcd( $a , $b - $a ); 
      } 
        
      // Function to count total numbers divisible 
      // by x but not y in range 1 to N
      function countNumbers($X, $Y, $N)
      {
        
          // Count total number divisible by X
          $divisibleByX = $N / $X;
        
          // Count total number divisible by Y
          $divisibleByY = $N /$Y;
        
          // Count total number divisible by either X or Y
          $LCM = ($X * $Y) / __gcd($X, $Y);
          $divisibleByLCM = $N / $LCM;
          $divisibleByXorY = $divisibleByX + $divisibleByY - 
                                             $divisibleByLCM;
        
          // Count total numbers divisible by X but not Y
          $divisibleByXnotY = $divisibleByXorY - 
                              $divisibleByY;
        
          return ceil($divisibleByXnotY);
      }
        
      // Driver Code
      $X = 2;
      $Y = 3;
      $N = 10;
      echo countNumbers($X, $Y, $N);
        
      // This is code contrubted by inder_verma
      ?>
      
      输出:
      4
      

      时间复杂度: O(1)