Answer the question
In order to leave comments, you need to log in
How to check the list for the uniqueness of the degree base?
How to make an algorithm to check the list for the uniqueness of the degree base? For example:
[16, 25, 125] is False because 25=5**2 and 125=5**3 [
2,7,7
] is False because there are duplicates
[2,5,43] is True
- algorithm execution speed
Answer the question
In order to leave comments, you need to log in
from math import gcd
def uniqueFactor(lst):
if len(set(lst)) != 3:
return False
lch=[]
for num in lst:
i=0; pr=prime[i]
while gcd(num,pr) != pr:
i+=1
pr=prime[i]
lch.append(pr)
if len(set(lch)) != 3:
return False
return True
In my case, prime is a list of prime numbers, in yours, it could be a simple range(X)
1) Remove duplicate elements and units.
2) Is the list empty or does it have one element? return true.
3) Select the smallest among the elements of the list -- a
4) Replace all elements of the list, except 'a', with the quotient of dividing this element by 'a'
5) Was there a non-zero remainder during division? Return "false"
6) Go to step 1
how it works.
* one is not an informative element, since any number to the zero power is 1, it can be excluded
* repeated elements are not informative, one of them can be left
* if the list consists of powers of one number: b^k1, b^k2,. .. b^kn, then
1) At each iteration, the product of the list elements will decrease until the algorithm terminates
2) the list will retain its property of the uniqueness of the base of the degree
* if the bases of the degree are not unique, at some point the remainder of the division will be non-zero
in essence on each iteration it is checked that the smallest element is not equal to 1 -- gcd for the entire list
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question