Pell numbers: Difference between revisions
Content added Content deleted
(New post.) |
(Added Python implementation for Pell numbers task) |
||
Line 1,655: | Line 1,655: | ||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python3"> |
|||
# pell_numbers.py by Xing216 |
|||
def is_prime(n): |
|||
if n == 1: |
|||
return False |
|||
i = 2 |
|||
while i*i <= n: |
|||
if n % i == 0: |
|||
return False |
|||
i += 1 |
|||
return True |
|||
def pell(p0: int,p1: int,its: int): |
|||
nums = [p0,p1] |
|||
primes = {} |
|||
idx = 2 |
|||
while len(nums) != its: |
|||
p = 2*nums[-1]+nums[-2] |
|||
if is_prime(p): |
|||
primes[idx] = p |
|||
nums.append(p) |
|||
idx += 1 |
|||
return nums, primes |
|||
def nsw(its: int,pell_nos: list): |
|||
nums = [] |
|||
for i in range(its): |
|||
nums.append(pell_nos[2*i] + pell_nos[2*i+1]) |
|||
return nums |
|||
def pt(its: int, pell_nos: list): |
|||
nums = [] |
|||
for i in range(1,its+1): |
|||
hypot = pell_nos[2*i+1] |
|||
shorter_leg = sum(pell_nos[:2*i+1]) |
|||
longer_leg = shorter_leg + 1 |
|||
nums.append((shorter_leg,longer_leg,hypot)) |
|||
return nums |
|||
pell_nos, pell_primes = pell(0,1,50) |
|||
pell_lucas_nos, pl_primes = pell(2,2,10) |
|||
nsw_nos = nsw(10, pell_nos) |
|||
pythag_triples = pt(10, pell_nos) |
|||
sqrt2_approx = {} |
|||
for idx, pell_no in enumerate(pell_nos): |
|||
numer = pell_nos[idx-1] + pell_no |
|||
if pell_no != 0: |
|||
sqrt2_approx[f"{numer}/{pell_no}"] = numer/pell_no |
|||
print(f"The first 10 Pell Numbers:\n {' '.join([str(_) for _ in pell_nos[:10]])}") |
|||
print(f"The first 10 Pell-Lucas Numbers:\n {' '.join([str(_) for _ in pell_lucas_nos])}") |
|||
print(f"The first 10 rational and decimal approximations of sqrt(2) ({(2**0.5):.10f}):") |
|||
print(" rational | decimal") |
|||
for rational in list(sqrt2_approx.keys())[:10]: |
|||
print(f"{rational:>10} ≈ {sqrt2_approx[rational]:.10f}") |
|||
print("The first 7 Pell Primes:") |
|||
print(" index | Pell Prime") |
|||
for idx, prime in pell_primes.items(): |
|||
print(f"{idx:>6} | {prime}") |
|||
print(f"The first 10 Newman-Shank-Williams numbers:\n {' '.join([str(_) for _ in nsw_nos])}") |
|||
print(f"The first 10 near isosceles right triangles:") |
|||
for i in pythag_triples: |
|||
print(f" {i}") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre style="height: 15em;"> |
|||
The first 10 Pell Numbers: |
|||
0 1 2 5 12 29 70 169 408 985 |
|||
The first 10 Pell-Lucas Numbers: |
|||
2 2 6 14 34 82 198 478 1154 2786 |
|||
The first 10 rational and decimal approximations of sqrt(2) (1.4142135624): |
|||
rational | decimal |
|||
1/1 ≈ 1.0000000000 |
|||
3/2 ≈ 1.5000000000 |
|||
7/5 ≈ 1.4000000000 |
|||
17/12 ≈ 1.4166666667 |
|||
41/29 ≈ 1.4137931034 |
|||
99/70 ≈ 1.4142857143 |
|||
239/169 ≈ 1.4142011834 |
|||
577/408 ≈ 1.4142156863 |
|||
1393/985 ≈ 1.4142131980 |
|||
3363/2378 ≈ 1.4142136249 |
|||
The first 7 Pell Primes: |
|||
index | Pell Prime |
|||
2 | 2 |
|||
3 | 5 |
|||
5 | 29 |
|||
11 | 5741 |
|||
13 | 33461 |
|||
29 | 44560482149 |
|||
41 | 1746860020068409 |
|||
The first 10 Newman-Shank-Williams numbers: |
|||
1 7 41 239 1393 8119 47321 275807 1607521 9369319 |
|||
The first 10 near isosceles right triangles: |
|||
(3, 4, 5) |
|||
(20, 21, 29) |
|||
(119, 120, 169) |
|||
(696, 697, 985) |
|||
(4059, 4060, 5741) |
|||
(23660, 23661, 33461) |
|||
(137903, 137904, 195025) |
|||
(803760, 803761, 1136689) |
|||
(4684659, 4684660, 6625109) |
|||
(27304196, 27304197, 38613965) |
|||
</pre> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||