Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:19:58+00:00 2026-06-15T22:19:58+00:00

For integers x, y, and n, (only x and y are given) test if

  • 0

For integers x, y, and n, (only x and y are given) test if xn = y? If x = 8, y = 512, then n = 3 so that is true. But if x = 8 and y = 500, n would have to be around 2.98 (not an integer) so the statement evaluates as false. Is using a logarithm the best method to do this test?

Check if one integer is an integer power of another offers some solutions:

int n = y; while(n < x) n *= y; return n == x,

while (x%y == 0)  x = x / y
return x == 1

and the logarithm method (this is my version):

return ((log(y, x) % 1) == 0) // log(expression, base)

log(y, x) = log x y

Which method evaluates faster, especially for big numbers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:19:59+00:00Added an answer on June 15, 2026 at 10:19 pm

    The logarithm method needs more care, because the logarithm function has a small amount of inaccuracy due to the use of floating-point approximations. For example 310 = 59049, but:

    log(59049, 3)
       ===> 9.999999999999998
    

    Whether you can compensate for this (by checking to see if the answer is “close enough” to the nearest integer) depends on the range of your x and y. If y is less than 232, then I think the closest the logarithm can proportionately get to an integer (without the true answer being an integer) is:

    1 - log(4294967295, 65536) / 2
       ===> 1.049693665322593e-11
    

    so pick an epsilon smaller than this and you can use the logarithm method with confidence:

    n = log(y, x);
    e = round(n);
    if (abs(1 - n / e) < epsilon) {
        /* y == x to the power of e */
    } else {
        /* y not a power of x */
    }
    

    If the allowable range for y is bigger, then you’ll have to find the appropriate value for epsilon. But beware: for large enough y, there might be no suitable epsilon in double-precision floating-point. For example, if y can be as large as 248 − 1 then that’s the case, because

    log(281474976710655, 16777216)
       ===> 2.0 exactly
    

    So for large enough y, you can’t rely on the logarithm: you’ll need to explicitly perform the exponentiation afterwards as a check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CGFloat that contains only integers, in the meaning that it actually
I have an array of Integers in Java, I would like use only a
Suppose I'm writing a function that takes a list of integers and returns only
OK, I have a table with no natural key, only an integer identity column
Given two integers a and b , is there an efficient way to test
I have a regex which allows only to enter integers and floats in a
I have a CSV file that has 2999 rows but on importing it to
Here are the files that I was given, I was told not to change
I have a file of true random bytes. I want a function that returns
This may not be a programming question but it's a problem that arised recently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.