📜  检查数字是否可被23整除

📅  最后修改于: 2021-05-06 08:04:29             🧑  作者: Mango

给定一个数字,任务是快速检查该数字是否可被23整除。

例子:

Input : x  = 46
Output : Yes

Input : 47
Output : No

解决该问题的方法是提取最后一位数字,并将最后一位数字的7倍加到剩余数字上,然后重复此过程,直到获得两位数字为止。如果获得的两位数可被23整除,则给定数字可被23整除。

方法:

  • 每次提取数字/截断数字的最后一位
  • 将7 *(前一个数字的最后一位数字)添加到截断后的数字中
  • 视需要重复上述三个步骤。

插图:

17043-->1704+7*3 
= 1725-->172+7*5
= 207 which is 9*23, 
so 17043 is also divisible by 23.
C++
// CPP program to validate above logic
#include 
using namespace std;
  
// Function to check if the number is
// divisible by 23 or not
bool isDivisible(long long int n) 
{
  
    // While there are at least 3 digits
    while (n / 100) 
    {
        int d = n % 10; // Extracting the last digit
        n /= 10; // Truncating the number
  
        // Adding seven times the last 
        // digit to the remaining number
        n += d * 7; 
    }
  
    return (n % 23 == 0);
}
  
int main()
{
    long long int n = 1191216;
    if (isDivisible(n))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}


Java
// Java program to validate above logic
class GFG
{
      
// Function to check if the 
// number is divisible by 
// 23 or not
static boolean isDivisible(long n) 
{
  
    // While there are at 
    // least 3 digits
    while (n / 100 != 0) 
    {
        // Extracting the last digit
        long d = n % 10; 
          
        n /= 10; // Truncating the number
  
        // Adding seven times the last 
        // digit to the remaining number
        n += d * 7; 
    }
  
    return (n % 23 == 0);
}
  
// Driver Code
public static void main(String[] args)
{
    long n = 1191216;
    if(isDivisible(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by mits


Python 3
# Python 3 program to validate above logic
  
# Function to check if the number is 
# divisible by 23 or not 
def isDivisible(n) :
  
    # While there are at least 3 digits
    while n // 100 :
  
        # Extracting the last
        d = n % 10
  
        # Truncating the number 
        n //= 10
  
        # Adding seven times the last  
        # digit to the remaining number 
        n += d * 7
  
    return (n % 23 == 0)
  
# Driver Code
if __name__ == "__main__" :
  
    n = 1191216
  
    # function calling
    if (isDivisible(n)) :
        print("Yes")
  
    else :
        print("No")
  
# This code is contributed by ANKITRAI1


C#
// C# program to validate 
// above logic
class GFG
{
      
// Function to check if the 
// number is divisible by 
// 23 or not
static bool isDivisible(long n) 
{
  
    // While there are at 
    // least 3 digits
    while (n / 100 != 0) 
    {
        // Extracting the last digit
        long d = n % 10; 
          
        n /= 10; // Truncating the number
  
        // Adding seven times the last 
        // digit to the remaining number
        n += d * 7; 
    }
  
    return (n % 23 == 0);
}
  
// Driver Code
public static void Main()
{
    long n = 1191216;
    if(isDivisible(n))
        System.Console.WriteLine("Yes");
    else
        System.Console.WriteLine("No");
}
}
  
// This code is contributed by mits


PHP


输出:
Yes

请注意,上面的程序可能没有多大意义,因为可以简单地执行n%23来检查可除性。该程序的目的是验证该概念。另外,如果输入数字很大并指定为字符串,那么这可能是一种有效的方法。