Python Forum
find 2 largest equal numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find 2 largest equal numbers
#8
This test does not result in a denominator.
d =  round(a / i)
if a == 42, d will have values 1, 2, 3, 4, 5, 6, 7, 8, 10, 14, 21, 42. The problem is that 4, 5, 8 and 10 do not divide evenly into 42. The correct test is.
for i in range(1, a+1):
    if a % i == 0:
        d = i
        print(d)
This correctly prints 1, 2, 3, 6, 7, 14, 21, 42. But there is another problem.

Even if you correctly identified denominators your code has no way to test for commonality. After printing the denominators of 42 your code has d == 42 and after printing the denominators for 154 your code has f == 154. 42 does not equal 154, so GCD is never printed. This is not a problem caused by missing curly brackets or an ignorance of Python. This is a logic error.

If you were solving this problem with pencil and paper you might write down all the denominators for 42 and all the denominators for154 and look for numbers that appear in both lists. In Python this looks like this:
def denominators(x):
    """Returns list of denominators for x"""
    return [d for d in range(1, x+1) if x % d == 0]

def common_denominators(x, y):
    """Returns list of values appearing in both lists x and y"""
    return [d for d in x if d in y]

d42 = denominators(42)
d154 = denominators(154)
common = common_denominators(d42, d154)

print("42", d42)
print("154", d154)
print("common", common[-1], common)
Output:
42 [1, 2, 3, 6, 7, 14, 21, 42] 154 [1, 2, 7, 11, 14, 22, 77, 154] common 14 [1, 2, 7, 14]
There are other ways to find what numbers are common to two lists using Python set operations. Set operations are a bit faster but as with most programming tasks the real speed improvement results from thinking about the problem and optimizing the procedure.

If you have two numbers 42 and 154 you know there are no common denominators greater than 42 and that computing denominators for numbers in the range 43 to 154 is a waste of time. It is also a waste of time testing if a number divides evenly into 154 if we know it does not divide evenly into 42. Putting some smart into the algorithm gives us this:
def common_denominators(x, y):
    if x > y:
        x, y = y, x  # Make x the smaller of the two numbers
    return [d for d in range(x, 0, -1) if x % d == 0 and y % d == 0]

print(common_denominators(42, 154))
Output:
[14, 7, 2, 1]
Reply


Messages In This Thread
find 2 largest equal numbers - by Frankduc - Jan-10-2022, 03:16 PM
RE: find 2 largest equal numbers - by Gribouillis - Jan-10-2022, 03:22 PM
RE: find 2 largest equal numbers - by Frankduc - Jan-10-2022, 03:28 PM
RE: find 2 largest equal numbers - by Gribouillis - Jan-10-2022, 04:09 PM
RE: find 2 largest equal numbers - by Frankduc - Jan-10-2022, 04:31 PM
RE: find 2 largest equal numbers - by perfringo - Jan-10-2022, 06:21 PM
RE: find 2 largest equal numbers - by bowlofred - Jan-10-2022, 04:59 PM
RE: find 2 largest equal numbers - by deanhystad - Jan-10-2022, 06:55 PM
RE: find 2 largest equal numbers - by Frankduc - Jan-10-2022, 07:01 PM
RE: find 2 largest equal numbers - by deanhystad - Jan-10-2022, 08:22 PM
RE: find 2 largest equal numbers - by Frankduc - Jan-11-2022, 01:59 PM
RE: find 2 largest equal numbers - by Gribouillis - Jan-10-2022, 09:13 PM
RE: find 2 largest equal numbers - by ibreeden - Jan-11-2022, 06:33 PM
RE: find 2 largest equal numbers - by Frankduc - Jan-11-2022, 07:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 558 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,491 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  is there equal syntax to "dir /s /b" kucingkembar 2 1,057 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Find numbers using Regex giddyhead 18 3,371 Jul-28-2022, 12:29 AM
Last Post: giddyhead
  Find and Replace numbers in String giddyhead 2 1,305 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Can a variable equal 2 things? Extra 4 1,573 Jan-18-2022, 09:21 PM
Last Post: Extra
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,695 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  Largest product in a grid (projecteuler problem11) tragical 1 2,339 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Extract the largest value from a group without replacement (beginner) preliator 1 2,126 Aug-12-2020, 01:56 PM
Last Post: DPaul
  frequency of largest number group anshumanmuj 5 3,068 Jun-22-2020, 04:51 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020