Determines whether an integer is the power of another integer.

Source: Internet
Author: User

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 ")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.