fork(1) download
  1. def gcd( a, b ):
  2. "greatest common divisor"
  3. while True:
  4. c = a % b
  5. if c < 1e-5:
  6. return b
  7. a, b = b, c
  8.  
  9. def gcdset( a_set ):
  10. "use the pairwise gcd to find gcd of a set"
  11. x = a_set.pop()
  12. total = x
  13. for u in a_set:
  14. x = gcd( u, x )
  15. # the following step is optional,
  16. # some sort of stabilization just for improved accuracy
  17. total = total + u
  18. x = total / round(total/x)
  19.  
  20. return x
  21.  
  22. # the list where we want to find the gcd
  23. inputlist = [2239.864226650253, 1250.4096410911607, 1590.1948696485413,
  24. 810.0479848807954, 2177.343744595695, 54.3656365691809, 2033.2748076873656,
  25. 2074.049035114251, 108.7312731383618, 2188.216871909531]
  26.  
  27. # we turn it into a set to get rid of duplicates
  28. aset = set(inputlist)
  29. print(gcdset( aset ))
Success #stdin #stdout 0.1s 10088KB
stdin
Standard input is empty
stdout
2.718281828459045