A Mathematical Question! (1 Viewer)

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 22:39
Joined
Mar 22, 2009
Messages
784
How we conclude whether a given number is a power of some number or Not?

Like we find the Divisors using mod operator? For Example: if 52 mod 26 = 0

Like that way I want to find if 676 pow 26 = 0

26^2 = 676

So Assume 26^x = 676.

I want to find the x or even an TRUE will suffice as I just want to confirm whether the number is falling in the power line or not
 
Last edited:

Orthodox Dave

Home Developer
Local time
Today, 18:09
Joined
Apr 13, 2017
Messages
218
Hi Prabha,

I think you need to put together a function that first gets all the prime factors of the big number, then works out if there is a precise square root, cube root etc.

For example, the number 676 has prime factors 2, 2, 13 and 13. In other words 676 = 2 X 2 X 13 X 13. So the square root is 2 X 13 = 26 - because all the factors are in pairs.

Prime factors of 4096 are 2 X 2 X 2 ... 12 times. That means it is 64 squared and also 16 cubed and also 8 power 4 and 4 power 6.

There is VBA code out there to get the prime factors of a number. Then with the list of factors, see if these group into pairs, trios etc - with no odd ones out. If the prime factors don't ALL group in this way, it is not an exact power.
 

plog

Banishment Pending
Local time
Today, 12:09
Joined
May 11, 2011
Messages
11,646
You description and sample doesn't make complete sense. I hate being pedantic on the internet but math issues require it:

How we conclude whether a given number is a power of some number or Not?

Do you mean 'root' instead of power? A root can be multipled by itself any number of times to get another number. 3 is a root of 9 and 27 and 81 and 243 etc.. Any number can be used as a power for another number, not every number can be a root of another.

Also:

Like that way I want to find if 702 pow 26 = 0

26^2 = 702

That's not right in 2 ways. 702 pow 26 means 702 raised to the 26th power, which can be expressed as 702^26 which is a humongous number. Further 26^2 means 26 raised to the 2nd power which is equivalent to 26*26 which is 676.

Now, if you want to determine if X is a root of Y (so that if X=3 and Y=27 the answer is yes) I think you can do that with a WHILE and a counter. Here's some psuedo code:

Code:
function Is_Root(root, target) AS Boolean
    ' determines if root is a root of target 

ret=false    
counter=0
rootcalc=1

WHILE rootcalc<target
    ++counter
    routcalc*=root
    loop

if rootcalc==target then ret=true
Is_Root = ret
End Function

If you wanted to know what power to raise root to, the variable counter would tell you that at the end of the WHILE loop.
 

Orthodox Dave

Home Developer
Local time
Today, 18:09
Joined
Apr 13, 2017
Messages
218
I think he had it right:

How we conclude whether a given number is a power of some number or Not?
i.e. how do we conclude whether, for example, 36 is a power of some number or not - yes it is the second power of 6 (6 X 6) - i.e. 6 squared. On the other hand 37 isn't a power of any whole number. The question is whether it has an exact root. I think that's what he means. It's 6:30pm in Mumbai so we may have to wait till tomorrow to find out!
 

static

Registered User.
Local time
Today, 18:09
Joined
Nov 2, 2015
Messages
823
Code:
Sub test()

    n = 676
    
    For i = 2 To n - 1
        If n / i = CInt(n / i) Then
            If i = n / i Then p = True Else p = False
            Debug.Print i, n / i, p
        End If
    Next
End Sub

gives

2 338 False
4 169 False
13 52 False
26 26 True
52 13 False
169 4 False
338 2 False
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:09
Joined
Feb 28, 2001
Messages
27,188
Without knowing what constraints exist on these numbers, the question is going to be difficult to answer. If we are asking the question:

Given the formula C = A^B and given the values of C and A, is this formula correct (TRUE) for some B?

This is inadequate as a problem statement unless we can add constraints such as "A, B, and C are positive integers." It is further constrained by representational issues since we have to work on the problem using data types available to us, which usually means LONG integers. So the question implies the practical constraint "A < about 2 ^ 10^9".

prabha_friend, we can make all sorts of assumptions but the question will always come down to what constraints may we assume before even attempting to attack the problem? I see others have attacked it by giving code samples, but we have to remember that if either A or B is NOT required to be an integer, the answer to the question is ALWAYS TRUE.
 

ChrisMacD

New member
Local time
Today, 14:09
Joined
Jul 12, 2017
Messages
1
First Post: So please forgive me if I get the formatting incorrect.
I think I understand the question. I have a function for both scenarios: is one number a power of another AND what is the power with a return of 0 if it doesn't have a whole number power.

Theory: If you keep dividing the larger number (NumAns) by the lower number (NumX) you will eventually get a number equal to or less than the lower number. If it's lower, then it's not a power. If it's equal, then it's a power.

For WhatIsPower, I change the value of NumAns to Single. If the result of the loop is 1 exactly, then it is a power and returns the count. if it's not, the it will return 0. 12^x = 248832 WhatIsPower(12,248832) will return 5.

This will only work on non-decimal numbers.

Code:
Public Function IsPowerOf(NumX As Long, NumAns As Long) As Boolean
    
[INDENT]Dim vResp As Long
IsPowerOf = False
Do
[INDENT]NumAns = NumAns / NumX
[/INDENT]Loop Until NumAns <= NumX
If NumAns = NumX Then IsPowerOf = True
[/INDENT]
End Function


Code:
Public Function WhatIsPower(NumX As Long, NumAns As Long) As Long
    
[INDENT]Dim vResp As Single
[/INDENT][INDENT]vResp = CSng(NumAns)

Dim myCount As Long
myCount = 0
WhatIsPower = 0
Do
[INDENT]vResp = vResp / NumX
myCount = myCount + 1
[/INDENT]Loop Until vResp < NumX
If vResp = 1 Then WhatIsPower = myCount
[/INDENT]
End Function


Let us know if that is what you are looking for.

Chris
 
Last edited:

MarkK

bit cruncher
Local time
Today, 10:09
Joined
Mar 17, 2004
Messages
8,181
After a little googling, it appears that you can solve for exponents using the log function, like...
Code:
Function GetExponent(Base As Single, Result As Single) As Single
    GetExponent = Log(Result) / Log(Base)
End Function
...so in the immediate pane you'd get...
Code:
? GetExponent(26, 676)
 2
? GetExponent(2, 65536)
 16 
? GetExponent(4, 2)
 0.5
...which looks right.
Mark
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:09
Joined
Feb 28, 2001
Messages
27,188
MarkK and Prabha_Friend,

The logarithmic result will work correctly for smaller numbers but remember that SINGLE limits you to about 7 digits of precision. There is such a thing as truncation due to representation size that can muddy the waters. If your input integers are LONG and you used DOUBLE for all logarithmic intermediates, you are better off. This is because a LONG can only represent 32 bits of integer (which is like a mantissa of 32 bits for which the exponent is 0). A DOUBLE can represent a mantissa of 55 bits. Therefore using the logarithmic function has the BEST chance of succeeding if you cast all intermediates as DOUBLE.

By contrast, if you were looking at numbers at the high end of a LONG, you can easily have numbers on the order of 100 million, but the SINGLE representation of such a number would be unable to distinguish between numbers that differ by 1 or 2 in the "ones" digit. The logarithms would be no better. Therefore, if you can have larger numbers as inputs, use DOUBLE intermediates, which WILL give your enough precision to make that work reasonably well.
 

Stormin

Nawly Ragistarad Usar
Local time
Today, 18:09
Joined
Dec 30, 2016
Messages
76
you can solve for exponents using the log function

For further reading (sorry I like maths ;) )... Logarithms and Exponential are two sides of the same coin, like Multiply and Divide.

My method for remembering the relation is (sorry there's no sub/superscript):
Code:
Base ^ Exp = Ans    <=>    Log[Base](Ans) = Exp
e.g.
Code:
26 ^ 2 = 676    <=>    Log[26](676) = 2
Unfortunately in VBA you can't specify the base for a log, it defaults to the constant e making everything a natural log.

As MarkK found, you can use same-base logs to calculate other base logs using the change-of-base formula:
Code:
Log[Base](Ans) = Log[y](Ans) / Log[y](Base)
And hence, if we are trying to find the Exponent using the Base and Ans:
Code:
Exp = Log[Base](Ans) = Log[y](Ans) / Log[y](Base)
Removing the middle brings us straight to the code that MarkK posted:
Code:
Exp = Log(Ans) / Log(Base)
Where in VBA, Log(x) = Log[e](x) = Ln(x)


For even further reading:
http://www.purplemath.com/modules/solvelog2.htm
http://www.purplemath.com/modules/logrules5.htm
https://support.office.com/en-gb/ar...45e-b2a3-cca03ec28b0b?ui=en-US&rs=en-GB&ad=GB
 

Stormin

Nawly Ragistarad Usar
Local time
Today, 18:09
Joined
Dec 30, 2016
Messages
76
I decided I needed to understand how it worked

A very quick explanation (as it's probably off-topic) would be that using A MOD B to find the GCD is a short way of doing the original 'subtract smallest from largest' method from 300 BCE.

Code:
We know that GCD([COLOR="Green"]35[/COLOR], [COLOR="Blue"]10[/COLOR]) = 5 by inspection
[COLOR="Green"](35 = 5 * 7)[/COLOR] - [COLOR="Blue"](10 = 5 * 2)[/COLOR] = [COLOR="Green"](25 = 5 * 5)[/COLOR]
[COLOR="Green"](25 = 5 * 5)[/COLOR] - [COLOR="Blue"](10 = 5 * 2)[/COLOR] = [COLOR="Green"](15 = 5 * 3)[/COLOR]
[COLOR="Green"](15 = 5 * 3)[/COLOR] - [COLOR="Blue"](10 = 5 * 2)[/COLOR] = [COLOR="Green"]( 5 = 5 * 1)[/COLOR]
[COLOR="Blue"](10 = 5 * 2)[/COLOR] - [COLOR="Green"]( 5 = 5 * 1)[/COLOR] = [COLOR="Blue"]( 5 = 5 * 1)[/COLOR]
Both parts reduced to the same number so GCD = 5

You can see that if there is a common factor, subtracting one from the other just subtracts multiples of the GCD. You stop one iteration before you would reach zero.
 

Users who are viewing this thread

Top Bottom