Python Forum
find 2 largest equal numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find 2 largest equal numbers
#10
Though short my solution is far from efficient. Given my two numbers 42 and 154 I know for sure that there will not be any common denominators in the range 22 to 41 or the range 15 to 20 or... My quick and dirty works fine if one of the numbers if fairly small, but there is a lot of needless division going on and this could result in long delays if both of your numbers are large.

Division is costly, so if you can do fewer divisions your algorithm is going to run faster. The 30 line GCD program you mention is probably using Stein's algorithm. Steins algorithm replaces division with shifts and subtraction. This requires a lot of testing to determine the next step in the algorithm, but you can do a lot of if statements in the time it takes to do 1 division.

Or maybe you saw Euclid's algorithm. Euclid's algorithm still does division, but is more efficient because it does less of it. But I don't think I've ever seen an implementation of Euclid's algorithm that is 30 lines long. Normally the code is pretty short.
def gcd(a, b):
  if b == 0:
    return a:
  else:
    return gcd(b, (a % b))
Or even shorter
def gcd(a, b):
    return a if b == 0 else gcd(b, (a % b))
Though this does less division than my example it does use recursion, so some of the performance gains are lost. It also doesn't generate a list or common denominators, just the greatest common denominator.
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 590 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,719 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  is there equal syntax to "dir /s /b" kucingkembar 2 1,086 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Find numbers using Regex giddyhead 18 3,501 Jul-28-2022, 12:29 AM
Last Post: giddyhead
  Find and Replace numbers in String giddyhead 2 1,356 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Can a variable equal 2 things? Extra 4 1,617 Jan-18-2022, 09:21 PM
Last Post: Extra
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,727 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  Largest product in a grid (projecteuler problem11) tragical 1 2,375 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Extract the largest value from a group without replacement (beginner) preliator 1 2,162 Aug-12-2020, 01:56 PM
Last Post: DPaul
  frequency of largest number group anshumanmuj 5 3,126 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