search
HomeJavaHow to write a function in Java to check if a number is a power of another number?

php editor Youzi brings you answers to Java programming questions: How to write a function in Java to check whether a number is the power of another number? Writing such a function will help you quickly and accurately determine the multiple relationship between numbers in a Java program, which will facilitate your programming work. In this article, we will explore how to write such a function using Java language and give detailed code implementation and examples. Let’s take a closer look!

Question content

I wrote a boolean function that checks whether the integer m is a power of n. But my code is incorrect. For example, 625 is a power of 5. But my code returns false.

public static boolean isPower(int m, int n) {
  if (m <= n) {
    return false;
  }
  int pow = n;
  while (pow <= m) {
    pow = n * pow;
    if (pow == m) {
      return true;
    }
    pow++;
  }
  return false;
}

Solution

  • A bug in your solution was pointed out in the comments.
  • Your code also doesn't handle the m = 1 case well.
  • For most inputs, the following methods can reduce the number of iterations:
// for n, m > 0
static boolean isPower(int m, int n) {
    while (m % n == 0) {
        m /= n;
    }
    
    return (m == 1);
}

Here I repeatedly divide m by n until I encounter a non-zero remainder. For exact powers of n, I end up with m = 1.

Using this method all possible m values ​​of (n - 1)/n will be rejected on the first iteration because n Only one number among consecutive numbers has m % n == 0.

The above is the detailed content of How to write a function in Java to check if a number is a power of another number?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version