给定一个数字,检查它是否可以被7整除。不允许使用模运算符,也不允许使用浮点运算。
一种简单的方法是重复减法。以下是另一种有趣的方法。
可以通过递归方法检查7除数。当且仅当a – 2b可被7整除时,形式10a + b的数字可被7整除。换句话说,从剩余数字形成的数字中减去最后一位的两倍。继续执行此操作,直到数量很少。
例如:数字371:37 –(2×1)= 37 – 2 = 35; 3 –(2×5)= 3 – 10 = -7;因此,由于-7可被7整除,因此371可被7整除。
以下是上述方法的实现
C++
// A Program to check whether a number is divisible by 7
#include
using namespace std;
int isDivisibleBy7( int num )
{
// If number is negative, make it positive
if( num < 0 )
return isDivisibleBy7( -num );
// Base cases
if( num == 0 || num == 7 )
return 1;
if( num < 10 )
return 0;
// Recur for ( num / 10 - 2 * num % 10 )
return isDivisibleBy7( num / 10 - 2 *
( num - num / 10 * 10 ) );
}
// Driver code
int main()
{
int num = 616;
if( isDivisibleBy7(num ) )
cout << "Divisible" ;
else
cout << "Not Divisible" ;
return 0;
}
// This code is contributed by rathbhupendra
C
// A Program to check whether a number is divisible by 7
#include
int isDivisibleBy7( int num )
{
// If number is negative, make it positive
if( num < 0 )
return isDivisibleBy7( -num );
// Base cases
if( num == 0 || num == 7 )
return 1;
if( num < 10 )
return 0;
// Recur for ( num / 10 - 2 * num % 10 )
return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) );
}
// Driver program to test above function
int main()
{
int num = 616;
if( isDivisibleBy7(num ) )
printf( "Divisible" );
else
printf( "Not Divisible" );
return 0;
}
Java
// Java program to check whether a number is divisible by 7
import java.io.*;
class GFG
{
// Function to check whether a number is divisible by 7
static boolean isDivisibleBy7(int num)
{
// If number is negative, make it positive
if( num < 0 )
return isDivisibleBy7( -num );
// Base cases
if( num == 0 || num == 7 )
return true;
if( num < 10 )
return false;
// Recur for ( num / 10 - 2 * num % 10 )
return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) );
}
// Driver program
public static void main (String[] args)
{
int num = 616;
if(isDivisibleBy7(num))
System.out.println("Divisible");
else
System.out.println("Not Divisible");
}
}
// Contributed by Pramod Kumar
Python
# Python program to check whether a number is divisible by 7
# Function to check whether a number is divisible by 7
def isDivisibleBy7(num) :
# If number is negative, make it positive
if num < 0 :
return isDivisibleBy7( -num )
# Base cases
if( num == 0 or num == 7 ) :
return True
if( num < 10 ) :
return False
# Recur for ( num / 10 - 2 * num % 10 )
return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) )
# Driver program
num = 616
if(isDivisibleBy7(num)) :
print "Divisible"
else :
print "Not Divisible"
# This code is contributed by Nikita Tiwari
C#
// C# program to check whether a
// number is divisible by 7
using System;
class GFG {
// Function to check whether a
// number is divisible by 7
static bool isDivisibleBy7(int num)
{
// If number is negative,
// make it positive
if( num < 0 )
return isDivisibleBy7(-num);
// Base cases
if( num == 0 || num == 7 )
return true;
if( num < 10 )
return false;
// Recur for ( num / 10 - 2 * num % 10 )
return isDivisibleBy7(num / 10 - 2 *
( num - num / 10 * 10 ));
}
// Driver Code
public static void Main ()
{
int num = 616;
if(isDivisibleBy7(num))
Console.Write("Divisible");
else
Console.Write("Not Divisible");
}
}
// This code is contributed by Nitin Mittal.
PHP
=0 )
echo("Divisible");
else
echo("Not Divisible");
// This code is contributed by vt_m.
?>
Javascript
输出:
Divisible
这是如何运作的?设“ b”为数字“ n”的最后一位,设“ a”为除以“ b”时得到的数字。
数字的表示形式也可以乘以除数相对质数的任何数字,而不会改变其除数。观察到7除以21后,我们可以执行以下操作:
10.a + b
乘以2后,变为
20.a + 2.b
然后
21.a - a + 2.b
消除21的倍数
-a + 2b
并乘以-1得到
a - 2b