Determines whether an integer is the power of another integer.
I recently learned about the 20 most popular questions about c # on the Internet in Microsoft's virtual course. I want to write down my personal understanding and solutions for this question.
Subject:(Original) determine whether a number is a power of 2?
This is my own solution (a small console program) without looking at the correct answer. The Code is as follows:
Static void Main (string [] args) {for (int increment = 0; increment <100000; increment ++) {if (IsPower (increment) {Console. writeLine (increment) ;}} Console. readKey ();} private static bool IsPower (int number) {bool result = false; if (number <= 0 | number % 2 = 1) return false; if (number/2> 1) {result = IsPower (number/2);} else {result = number % 2 = 0;} return result ;}View Code
My solution is to use a recursive method to call myself and determine whether the last call method is a power.
The solutions provided by Microsoft are as follows:
Static void Main (string [] args) {for (int increment = 0; increment <100000; increment ++) {if (isPowerFromMic (increment) {Console. writeLine (increment) ;}} Console. readKey ();} private static bool isPowerFromMic (int number) {return number> 1 & (number-1) = 0 );}View Code
After reading their solutions, I felt that my solutions were weak and explosive! This idea depends on the binary principle of the computer, for example:
I will briefly explain that when the first digit is always 1 after each forward and last step, and the digits are all 0, for example, 8 & 7 = 0. It is easy to understand.
From this, you can use questions to get a question (Microsoft's method seems to be not conducive to expansion and slightly modified its code ):
Determines whether an integer is the power of another number? What about 2?
Static void Main (string [] args) {while (true) {int baseNumber = Int32.Parse (Console. readLine (); int powerNumber = Int32.Parse (Console. readLine (); Console. writeLine (IsPower (baseNumber, powerNumber) ;}} private static bool IsPower (int number, int powerNumber) {bool result = false; if (number <= 0 | number % powerNumber! = 0) return false; if (number/powerNumber> 1) {result = IsPower (number/powerNumber, powerNumber);} else {result = number % powerNumber = 0 ;} return result ;}View Code
How can I determine whether a number is an integer?
If the is_int () function is not used, it seems that it cannot be determined. You can try a regular expression.
<? Php
$ Num = @ $ _ GET ['num'];
$ Match = "/^ \-? [1-9] {1} [0-9] * $ | ^ [0] {1} $/"; // Match All integers (self-written regular expressions, is the main user using the address bar? Num = xxxx pass the parameter verification to this page)
If (preg_match ($ match, $ num )){
Echo "{$ num} is an integer .";
} Else {
Echo "{$ num} is not an integer .";
}
How does excel determine whether one number is an integer divided by the other?
Assume that the two numbers are in A1 B1.
Then C1 input = IF (MOD (A1, B1) = 0, "integer", "remainder ")