给定一个数字n,使用位运算符检查它是否可被17整除。 例如:
null
Input : n = 34Output : 34 is divisible by 17Input : n = 43Output : 43 is not divisible by 17
A. 幼稚的方法 如果剩下0,将由%operator检查。 使用除法 位操作符 ,我们必须用2的幂重写这个表达式。
n/17 = (16*n)/(17*16) = (17 - 1)*n/(17*16) = (n/16) - (n/(17*16))
我们可以使用除法的一般规则将n/16改写为floor(n/16)+(n%16)/16。
n/17 = floor(n/16) + (n%16)/16 - (floor(n/16) + (n%16)/16)/17 = floor(n/16) - (floor(n/16) - 17*(n%16)/16 + (n%16)/16)/17 = floor(n/16) - (floor(n/16)-n%16)/17
这个等式的左边是n/17。只有当右边是整数时,它才是整数。根据定义,楼层(n/16)是一个整数。如果(floor(n/16)-n%16)/17也是一个整数,那么整个左手边就是一个整数。 这意味着如果(楼层(n/16)-n%16)可被17整除,则n可被17整除。 (楼层(n/16)-n%16) 可以按位写入 (国际)(n>>4)–(国际)(n&15) 其中n>>4表示n/16,n&15表示n%16 以下是上述方法的实施情况:
CPP
// CPP program to check if a number is // divisible by 17 or not using bitwise // operator. #include <bits/stdc++.h> using namespace std; // function to check recursively if the // number is divisible by 17 or not bool isDivisibleby17( int n) { // if n=0 or n=17 then yes if (n == 0 || n == 17) return true ; // if n is less then 17, not // divisible by 17 if (n < 17) return false ; // reducing the number by floor(n/16) // - n%16 return isDivisibleby17(( int )(n >> 4) - ( int )(n & 15)); } // driver code to check the above function int main() { int n = 35; if (isDivisibleby17(n)) cout << n << " is divisible by 17" ; else cout << n << " is not divisible by 17" ; return 0; } |
JAVA
// Java program to check if a number is // divisible by 17 or not using bitwise // operator. class GFG{ // function to check recursively if the // number is divisible by 17 or not static boolean isDivisibleby17( int n) { // if n=0 or n=17 then yes if (n == 0 || n == 17 ) return true ; // if n is less then 17, not // divisible by 17 if (n < 17 ) return false ; // reducing the number by // floor(n/16) - n%16 return isDivisibleby17(( int )(n >> 4 ) - ( int )(n & 15 )); } // driver function public static void main(String[] args) { int n = 35 ; if (isDivisibleby17(n) == true ) System.out.printf ( "%d is divisible by 17" ,n); else System.out.printf ( "%d is not divisible by 17" ,n); } } // This code is contributed by // Smitha Dinesh Semwal |
Python3
# Python 3 program to # check if a number is # divisible by 17 or # not using bitwise # operator. # function to check recursively if the # number is divisible by 17 or not def isDivisibleby17(n): # if n=0 or n=17 then yes if (n = = 0 or n = = 17 ): return True # if n is less then 17, not # divisible by 17 if (n < 17 ): return False # reducing the number by floor(n/16) # - n%16 return isDivisibleby17(( int )(n >> 4 ) - ( int )(n & 15 )) # driver code to check the above function n = 35 if (isDivisibleby17(n)): print (n, "is divisible by 17" ) else : print (n, "is not divisible by 17" ) # This code is contributed by # Smitha Dinesh Semwal |
C#
// C# program to check if a number is // divisible by 17 or not using bitwise // operator. using System; class GFG { // function to check recursively if the // number is divisible by 17 or not static bool isDivisibleby17( int n) { // if n=0 or n=17 then yes if (n == 0 || n == 17) return true ; // if n is less then 17, not // divisible by 17 if (n < 17) return false ; // reducing the number by // floor(n/16) - n%16 return isDivisibleby17(( int )(n >> 4) - ( int )(n & 15)); } // Driver function public static void Main() { int n = 35; if (isDivisibleby17(n) == true ) Console.WriteLine (n + "is divisible by 17" ); else Console.WriteLine ( n+ " is not divisible by 17" ); } } // This code is contributed by // vt_m |
PHP
<?php // php program to check if a // number is divisible by 17 // or not using bitwise // operator. // function to check recursively // if the number is divisible // by 17 or not function isDivisibleby17( $n ) { // if n=0 or n=17 then yes if ( $n == 0 || $n == 17) return true; // if n is less then 17, not // divisible by 17 if ( $n < 17) return false; // reducing the number by floor(n/16) // - n%16 return isDivisibleby17((int)( $n >> 4) - (int)( $n & 15)); } // Driver Code $n = 35; if (isDivisibleby17( $n )) echo $n . " is divisible by 17" ; else echo $n . " is not divisible by 17" ; // This code is contributed by mits ?> |
Javascript
<script> // JavaScript program to check if a number is // divisible by 17 or not using bitwise // operator. // function to check recursively if the // number is divisible by 17 or not function isDivisibleby17(n) { // if n=0 or n=17 then yes if (n == 0 || n == 17) return true ; // if n is less then 17, not // divisible by 17 if (n < 17) return false ; // reducing the number by floor(n/16) // - n%16 return isDivisibleby17(Math.floor(n >> 4) - Math.floor(n & 15)); } // driver code to check the above function let n = 35; if (isDivisibleby17(n)) document.write(n + " is divisible by 17" ); else document.write(n + " is not divisible by 17" ); // This code is contributed by Surbhi Tyagi. </script> |
输出:
35 is not divisible by 17
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END