Debian Codegraph @

src/rackdiag/utils/math.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright 2011 Takeshi KOMIYA
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from __future__ import division
17
18
19 def gcd(*args):
20 if len(args) == 0:
21 return 0
22 elif len(args) == 1:
23 return args[0]
24 elif len(args) == 2:
25 if args[1] == 0:
26 return args[0]
27 else:
28 return gcd(args[1], args[0] % args[1])
29 else:
30 return gcd(gcd(*args[:-1]), args[-1])
31
32
33 def lcm(*args):
34 if len(args) == 0:
35 return 0
36 elif len(args) == 1:
37 return args[0]
38 elif len(args) == 2:
39 return args[0] * args[1] // gcd(*args)
40 else:
41 return lcm(lcm(*args[:-1]), args[-1])