给定一个数字,任务是快速检查该数字是否可被17整除。
例子:
Input : x = 34
Output : Yes
Input : x = 47
Output : No
解决该问题的方法是提取最后一位数字,并从剩余数字中减去最后一位数字的5倍,然后重复此过程,直到获得两位数字为止。如果获得的两位数可以被17整除,则给定的数字可以被17整除。
方法:
- 每次提取数字/截断数字的最后一位
- 从截断的数字中减去5 *(前一个数字的最后一位)
- 视需要重复上述三个步骤。
插图:
3978-->397-5*8=357-->35-5*7=0.
So 3978 is divisible by 17.
Mathematical Proof :
Let be any number such that =100a+10b+c .
Now assume that is divisible by 17. Then
0 (mod 17)
100a+10b+c 0 (mod 17)
10(10a+b)+c 0 (mod 17)
10+c 0 (mod 17)
Now that we have separated the last digit from the number, we have to find a way to use it.
Make the coefficient of 1.
In other words, we have to find an integer such that n such that 10n1 mod 17.
It can be observed that the smallest n which satisfies this property is -5 as -501 mod 17.
Now we can multiply the original equation 10+c 0 (mod 17)
by -5 and simplify it:
-50-5c 0 (mod 17)
-5c 0 (mod 17)
We have found out that if 0 (mod 17) then,
-5c 0 (mod 17).
In other words, to check if a 3-digit number is divisible by 17,
we can just remove the last digit, multiply it by 5,
and then subtract it from the rest of the two digits.
程序 :
C++
// CPP Program to validate the above logic
#include
using namespace std;
// Function to check if the
// number is divisible by 17 or not
bool isDivisible(long long int n)
{
while (n / 100)
{
// Extracting the last digit
int d = n % 10;
// Truncating the number
n /= 10;
// Subtracting the five times the
// last digit from the remaining number
n -= d * 5;
}
// Return n is divisible by 17
return (n % 17 == 0);
}
// Driver code
int main()
{
long long int n = 19877658;
if (isDivisible(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// Java Program to validate the above logic
import java.io.*;
class GFG {
// Function to check if the
// number is divisible by 17 or not
static boolean isDivisible(long n)
{
while (n / 100>0)
{
// Extracting the last digit
long d = n % 10;
// Truncating the number
n /= 10;
// Subtracting the five times the
// last digit from the remaining number
n -= d * 5;
}
// Return n is divisible by 17
return (n % 17 == 0);
}
// Driver code
public static void main (String[] args) {
long n = 19877658;
if (isDivisible(n))
System.out.println( "Yes");
else
System.out.println( "No");
}
}
// This code is contributed by inder_verma.
Python 3
# Python 3 Program to validate
# the above logic
# Function to check if the
# number is divisible by 17 or not
def isDivisible(n) :
while(n // 100) :
# Extracting the last digit
d = n % 10
# Truncating the number
n //= 10
# Subtracting the five times
# the last digit from the
# remaining number
n -= d * 5
# Return n is divisible by 17
return (n % 17 == 0)
# Driver Code
if __name__ == "__main__" :
n = 19877658
if isDivisible(n) :
print("Yes")
else :
print("No")
# This code is contributed
# by ANKITRAI1
C#
// C# Program to validate the above logic
using System;
class GFG {
// Function to check if the
// number is divisible by 17 or not
static bool isDivisible(long n)
{
while (n / 100>0)
{
// Extracting the last digit
long d = n % 10;
// Truncating the number
n /= 10;
// Subtracting the five times the
// last digit from the remaining number
n -= d * 5;
}
// Return n is divisible by 17
return (n % 17 == 0);
}
// Driver code
public static void Main () {
long n = 19877658;
if (isDivisible(n))
Console.Write( "Yes");
else
Console.Write( "No");
}
}
PHP
Yes
请注意,上面的程序可能没有多大意义,因为可以简单地执行n%23来检查可除性。该程序的目的是验证该概念。另外,如果输入数字很大并指定为字符串,那么这可能是一种有效的方法。