Wolstenholme numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: doh. typos) |
|||
Line 25: | Line 25: | ||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python">""" rosettacode.orgwiki/Magnanimous_numbers """ |
|||
from fractions import Fraction |
|||
from sympy import isprime |
|||
def wolstenholme(k): |
|||
""" Wolstenholme numbers """ |
|||
return sum(Fraction(1, i*i) for i in range(1, k+1)).numerator |
|||
def lineprintbig(bignum, digitstoshow=60, endchar="\n"): |
|||
""" Print a large number on one line, show beginning / end and number of digits """ |
|||
i, wstr = max(digitstoshow // 2, 5), str(bignum) |
|||
strlen = len(wstr) |
|||
if strlen > digitstoshow: |
|||
print(wstr[:i], '...', wstr[-i-1:], f'({strlen} digits)', end=endchar) |
|||
else: |
|||
print(wstr, end=endchar) |
|||
def process_wolstenholmes(nmax): |
|||
""" Run the tasks at rosettacode.org/wiki/Wolstenholme_numbers """ |
|||
wols = [wolstenholme(n) for n in range(1, nmax+1)] |
|||
print('First 20 Wolstenholme numbers:') |
|||
for i in wols[:20]: |
|||
lineprintbig(i) |
|||
print('\nFour Wolstenholme primes:') |
|||
for j in filter(isprime, wols): |
|||
lineprintbig(j) |
|||
process_wolstenholmes(20) |
|||
</syntaxhighlight>{{out}} |
|||
<pre> |
|||
First 20 Wolstenholme numbers: |
|||
1 |
|||
5 |
|||
49 |
|||
205 |
|||
5269 |
|||
5369 |
|||
266681 |
|||
1077749 |
|||
9778141 |
|||
1968329 |
|||
239437889 |
|||
240505109 |
|||
40799043101 |
|||
40931552621 |
|||
205234915681 |
|||
822968714749 |
|||
238357395880861 |
|||
238820721143261 |
|||
86364397717734821 |
|||
17299975731542641 |
|||
Four Wolstenholme primes: |
|||
5 |
|||
266681 |
|||
40799043101 |
|||
86364397717734821 |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |