检查一个数字是否可以被23整除

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

null
Input : x  = 46Output : YesInput : 47Output : 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.

数学证明: 允许 overline{a b c}  有多少这样的数字 overline{a b c}  =100a+10b+c。 现在假设 overline{a b c}  可以被23整除。然后 overline{a b c}equiv  0(mod 23) 100a+10b+c equiv  0(mod 23) 10(10a+b)+c equiv  0(mod 23) 10 overline{a b}  +c equiv  0(mod 23) 现在我们已经把最后一个数字和数字分开了,我们必须找到一种使用它的方法。 使系数 overline{a b}  1. 换句话说,我们必须找到一个整数,这样n,这样10n equiv  1 mod 23。 可以观察到,满足这一性质的最小n为7,即70 equiv  1 mod 23。 现在我们可以把原来的方程式乘以10 overline{a b}  +c equiv  0(mod 23) 通过7并简化它: 70 overline{a b}  +7c equiv  0(mod 23) overline{a b}  +7c equiv  0(mod 23) 我们发现如果 overline{a b c}equiv  0(mod 23)然后, overline{a b}  +7c equiv  0(mod 23)。 换句话说,要检查一个3位数是否可以被23整除, 我们可以去掉最后一个数字,乘以7, 然后从剩下的两位数中减去。

C++

// CPP program to validate above logic
#include <iostream>
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

<?php
// PHP program to validate above logic
// Function to check if the number
// is divisible by 23 or not
function isDivisible( $n )
{
// While there are at
// least 3 digits
while ( intval ( $n / 100))
{
$n = intval ( $n );
$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);
}
$n = 1191216;
if (isDivisible( $n ))
echo "Yes" . "" ;
else
echo "No" . "" ;
// This code is contributed
// by ChitraNayal
?>


Javascript

<script>
// JavaScript program to validate above logic
// Function to check if the
// number is divisible by
// 23 or not
function isDivisible(n)
{
// While there are at
// least 3 digits
while (Math.floor(n / 100) != 0)
{
// Extracting the last digit
let d = n % 10;
n = Math.floor(n/10); // Truncating the number
// Adding seven times the last
// digit to the remaining number
n += d * 7;
}
return (n % 23 == 0);
}
// Driver Code
let n = 1191216;
if (isDivisible(n))
document.write( "Yes" );
else
document.write( "No" );
// This code is contributed by rag2127
</script>


输出:

Yes

请注意,上述程序可能没有多大意义,因为只需执行n%23即可检查整除性。这个项目的目的是验证这个概念。此外,如果输入数字较大且以字符串形式给出,这可能是一种有效的方法。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享