Wolstenholme numbers: Difference between revisions

m
→‎{{header|Python}}: add stretch task
(Added Perl)
m (→‎{{header|Python}}: add stretch task)
Line 424:
 
from fractions import Fraction
from sympyitertools import isprimeaccumulate
from gmpy2 import is_prime
 
 
def wolstenholme(k):
""" Get the first k Wolstenholme numbers """
return sum(Fraction(1, i*i) for i in range(1, k+1))[r.numerator
for r in accumulate((Fraction(1, i*i) for i in range(1, k+1)), lambda x, y: x+y)]
 
 
def process_wolstenholmes(nmax):
def abbreviated(wstr, thresh=60, term='digits'):
""" return an abbreviated string with beginning / end and actual number of chars """
i, wlen = max(thresh // 2, 5), len(wstr)
return wstr if wlen < thresh else ' '.join([wstr[:i], '...', wstr[-i-1:], str(wlen), term])
 
 
def process_wolstenholmes(nmax):
""" Run the tasks at rosettacode.org/wiki/Wolstenholme_numbers """
wols = [wolstenholme(n10000) for n in range(1, nmax+1)]
print('FirstWolstenholme numbers 1 through 20, Wolstenholme500, numbers1000, 2500, 5000, 10000:')
for i in wolslist(range(1, 21)) + [:20500, 1000, 2500, 5000, 10000]:
print(f'{i:5d}: {abbreviated(str(wols[i-1]))}')
print('\nFournFifteen Wolstenholme primes:')
for ji, num in enumerate(filter(isprimeis_prime, wols[:2000])):
print(jf'{i+1:2d}: {abbreviated(str(num))}')
 
 
process_wolstenholmes(20)
</syntaxhighlight>{{out}}
<pre>
Wolstenholme numbers 1 through 20, 500, 1000, 2500, 5000, 10000:
First 20 Wolstenholme numbers:
1: 1
1
2: 5
5
3: 49
4: 205
5: 5269
6: 5369
7: 266681
8: 1077749
9: 9778141
10: 1968329
11: 239437889
12: 240505109
13: 40799043101
14: 40931552621
15: 205234915681
16: 822968714749
17: 238357395880861
18: 238820721143261
19: 86364397717734821
20: 17299975731542641
500: 409896675094170203645010936925 ... 0081053712248084984597965892703 434 digits
1000: 835459384831496894781878542648 ... 2396236858699094240207812766449 866 digits
2500: 645379119002306120908498982314 ... 6779664291212785535902976933153 2164 digits
5000: 344720865974885377161982285737 ... 0347952420022525144829082590451 4340 digits
10000: 547144231739333439995821771601 ... 0312824537149175649667700005717 8693 digits
 
FourFifteen Wolstenholme primes:
1: 5
5
2: 266681
3: 40799043101
4: 86364397717734821
5: 361909085967808623232911476131 ... 2280300268379995976006474252029 104 digits
6: 334279880945246012373031736295 ... 5361841612048446489305085140033 156 digits
7: 228127047583920023537742255081 ... 7225367354384405125167217413149 218 digits
8: 283476874732087929185503884347 ... 1887987141045794572911130248059 318 digits
9: 784405594406444260172907289698 ... 7380014755630422337523878698419 520 digits
10: 227068939751219255313726661483 ... 9454324603702173859396183964989 649 digits
11: 273103948085858989688057737445 ... 3536939403886311385662644388271 935 digits
12: 130010727366420487511145995120 ... 9791520578008635832246554146071 984 digits
13: 150868633053914560029346562867 ... 5398810094405367804007944918693 1202 digits
14: 235419351872699791002285017174 ... 2993590081502324742766220468879 1518 digits
15: 403067831438716075992501031099 ... 8022698298658901192511859288941 1539 digits
</pre>
 
4,102

edits