aboutsummaryrefslogtreecommitdiff
path: root/challenge-167/eric-cheung/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-167/eric-cheung/python')
-rwxr-xr-xchallenge-167/eric-cheung/python/ch-1.py50
-rwxr-xr-xchallenge-167/eric-cheung/python/ch-2.py42
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-167/eric-cheung/python/ch-1.py b/challenge-167/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..d832c4ff53
--- /dev/null
+++ b/challenge-167/eric-cheung/python/ch-1.py
@@ -0,0 +1,50 @@
+
+import math
+
+arrCirPrime = []
+
+def IsPrime(nInput):
+
+ for nDiv in range(2, int(math.sqrt(nInput)) + 1):
+ if nInput % nDiv == 0:
+ return False
+
+ return True
+
+
+def IsCircularPrime(nNum):
+
+ if not IsPrime(nNum):
+ return False
+
+ strNum = str(nNum)
+ nLen = len(strNum)
+
+ for nSubLoop in range(0, nLen):
+ strNum = strNum[1:] + strNum[0]
+ nVal = int(strNum)
+
+ if not IsPrime(nVal):
+ return False
+
+ nExistCount = arrCirPrime.count(nVal)
+
+ if nExistCount > 0:
+ return False
+
+ arrCirPrime.append(nNum)
+
+ return True
+
+
+nCount = 0
+nLoop = 101
+
+while nCount < 10:
+
+ if IsCircularPrime(nLoop):
+ print (str(nLoop))
+ nCount = nCount + 1
+
+ nLoop = nLoop + 1
+
diff --git a/challenge-167/eric-cheung/python/ch-2.py b/challenge-167/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..f6f799c698
--- /dev/null
+++ b/challenge-167/eric-cheung/python/ch-2.py
@@ -0,0 +1,42 @@
+
+## Remarks & Credit
+## https://en.wikipedia.org/wiki/Lanczos_approximation
+
+from cmath import sin, sqrt, pi, exp
+
+p = [676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6,1.5056327351493116e-7]
+
+EPSILON = 1e-07
+
+def drop_imag(z):
+ if abs(z.imag) <= EPSILON:
+ z = z.real
+
+ return z
+
+def gamma(z):
+ z = complex(z)
+
+ if z.real < 0.5:
+ y = pi / (sin(pi * z) * gamma(1 - z)) ## Reflection formula
+ else:
+ z = z - 1
+ x = 0.99999999999980993
+
+ for (i, pval) in enumerate(p):
+ x = x + pval / (z + i + 1)
+
+ t = z + len(p) - 0.5
+ y = sqrt(2 * pi) * t ** (z + 0.5) * exp(-t) * x
+
+ return drop_imag(y)
+
+"""
+The above use of the reflection (thus the if-else structure) is necessary, even though
+it may look strange, as it allows to extend the approximation to values of z where
+Re(z) < 0.5, where the Lanczos method is not valid.
+"""
+
+print(gamma(3))
+print(gamma(5))
+print(gamma(7))