给定数字N ,任务是检查数字是否可被47整除。
例子:
Input: N = 1645
Output: yes
Explanation:
47 * 35 = 1645
Input: N = 4606
Output: yes
Explanation:
47 * 98 = 4606
方法: 47的可除性检验为:
- 提取最后一位数字。
- 从除去最后一位后获得的剩余号码中减去14 *最后一位。
- 重复上述步骤,直到获得两位数字或零。
- 如果两位数可被47整除或为0,则原始数字也可被47整除。
例如:
If N = 59173
Step 1:
N = 59173
Last digit = 3
Remaining number = 5917
Subtracting 14 times last digit
Resultant number = 5917 - 14*3 = 5875
Step 2:
N = 5875
Last digit = 5
Remaining number = 587
Subtracting 14 times last digit
Resultant number = 587 - 14*5 = 517
Step 3:
N = 517
Last digit = 7
Remaining number = 51
Subtracting 14 times last digit
Resultant number = 51 - 14*7 = -47
Step 4:
N = -47
Since N is a two-digit number,
and -47 is divisible by 47
Therefore N = 59173 is also divisible by 47
下面是上述方法的实现:
C++
// C++ program to check whether a number
// is divisible by 47 or not
#include
#include
using namespace std;
// Function to check if the number is divisible by 47 or not
bool isDivisible(int n)
{
int d;
// While there are at least two digits
while (n / 100)
{
// Extracting the last
d = n % 10;
// Truncating the number
n /= 10;
// Subtracting fourteen times the last
// digit to the remaining number
n = abs(n-(d * 14));
}
// Finally return if the two-digit
// number is divisible by 47 or not
return (n % 47 == 0) ;
}
// Driver Code
int main() {
int N = 59173;
if (isDivisible(N))
cout<<"Yes"<
Java
// Java program to check whether a number
// is divisible by 47 or not
import java.util.*;
class GFG{
// Function to check if the number is divisible by 47 or not
static boolean isDivisible(int n)
{
int d;
// While there are at least two digits
while ((n / 100) > 0)
{
// Extracting the last
d = n % 10;
// Truncating the number
n /= 10;
// Subtracting fourteen times the last
// digit to the remaining number
n = Math.abs(n - (d * 14));
}
// Finally return if the two-digit
// number is divisible by 47 or not
return (n % 47 == 0) ;
}
// Driver Code
public static void main(String[] args) {
int N = 59173;
if (isDivisible(N))
System.out.print("Yes") ;
else
System.out.print("No");
}
}
// This code is contributed by PrinciRaj1992
Python 3
# Python program to check if a number
# is divisible by 47 or not
# Function to check if the number is
# divisible by 47 or not
def isDivisible(n) :
# While there are at least two digits
while n // 100 :
# Extracting the last
d = n % 10
# Truncating the number
n //= 10
# Subtracting fourteen times the last
# digit to the remaining number
n = abs(n- (d * 14))
# Finally return if the two-digit
# number is divisible by 43 or not
return (n % 47 == 0)
# Driver Code
if __name__ == "__main__" :
n = 59173
if (isDivisible(n)) :
print("Yes")
else :
print("No")
C#
// C# program to check whether a number
// is divisible by 47 or not
using System;
class GFG
{
// Function to check if the number is divisible by 47 or not
static bool isDivisible(int n)
{
int d;
// While there are at least two digits
while (n / 100 > 0)
{
// Extracting the last
d = n % 10;
// Truncating the number
n /= 10;
// Subtracting fourteen times the last
// digit to the remaining number
n = Math.Abs(n - (d * 14));
}
// Finally return if the two-digit
// number is divisible by 47 or not
return (n % 47 == 0);
}
// Driver Code
public static void Main()
{
int N = 59173;
if (isDivisible(N))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by mohit kumar 29.
输出:
Yes