Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to compare multiplications of powers of primes

Is there an algorithm to compare two numeric expressions that follow these rules:

  1. The expressions are multiplications of powers.
  2. Every power is a power of a prime number.
  3. The two expressions do not share any primes.

The comparison in question is comparison of the resulting multiplication products - which is larger?

An example of two such expressions is: (7^21)*(2^49) and (5^26)*(3^31). The idea is to compare such expressions with the powers being quite large, up to 2^64.

like image 380
Sergei Avatar asked Oct 31 '25 11:10

Sergei


1 Answers

As commented, you can use the log() function and create a compare method:

import math

def _cmp(A, B):
    asum = sum(a * math.log(p) for p, a in A)
    bsum = sum(b * math.log(q) for q, b in B)
    if asum > bsum:
        return 'A > B'
    elif asum < bsum:
        return 'A < B'
    else:
        return 'A = B'


A = [(7, 21), (2, 49)]
B = [(5, 26), (3, 31)]

print(_cmp(A, B))

Prints

A < B

like image 64
Aicody Avatar answered Nov 03 '25 01:11

Aicody