Wolstenholme numbers: Difference between revisions
Content deleted Content added
m →{{header|Python}}: simplify output functions |
J draft |
||
Line 24: | Line 24: | ||
;* [[oeis:A123751|OEIS:A123751 - Prime Wolstenholme numbers]] |
;* [[oeis:A123751|OEIS:A123751 - Prime Wolstenholme numbers]] |
||
=={{header|J}}== |
|||
<syntaxhighlight lang=J>NB. the first y members of the Wholstenhome sequence |
|||
WolstenholmeSeq=: {{ 2 {."1@x:+/\%*:1x+i.y }} |
|||
WolstenholmeSeq 20 |
|||
1 5 49 205 5269 5369 266681 1077749 9778141 1968329 239437889 240505109 40799043101 40931552621 205234915681 822968714749 238357395880861 238820721143261 86364397717734821 17299975731542641 |
|||
(#~ 1 p: ])WolstenholmeSeq 20 |
|||
5 266681 40799043101 86364397717734821 |
|||
</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 07:43, 3 May 2023
Wolstenholme numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
A Wolstenholme number is a number that is the fully reduced numerator of a second order harmonic number:
Prime Wolstenholme numbers are Wolstenholme numbers that are prime.
(Oddly enough, there is also a class of numbers called Wolstenholme primes that are not Wolstenholme numbers that are prime. This is not them.)
- Task
- Find and display the first 20 Wolstenholme numbers. (Or as many as reasonably supported by your language if it is fewer.)
- Find and display the first 4 prime Wolstenholme numbers. (Or as many as reasonably supported by your language if it is fewer.)
- Stretch
- Find and display the digit count of the 500th, 1000th, 2500th, 5000th, 10000th Wolstenholme numbers.
- Find and display the digit count of the first 15 prime Wolstenholme numbers.
- See also
J
NB. the first y members of the Wholstenhome sequence
WolstenholmeSeq=: {{ 2 {."1@x:+/\%*:1x+i.y }}
WolstenholmeSeq 20
1 5 49 205 5269 5369 266681 1077749 9778141 1968329 239437889 240505109 40799043101 40931552621 205234915681 822968714749 238357395880861 238820721143261 86364397717734821 17299975731542641
(#~ 1 p: ])WolstenholmeSeq 20
5 266681 40799043101 86364397717734821
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 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]:
print(i)
print('\nFour Wolstenholme primes:')
for j in filter(isprime, wols):
print(j)
process_wolstenholmes(20)
- Output:
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
Raku
use Lingua::EN::Numbers;
sub abbr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '..' ~ .substr(*-20) ~ " (digits: {.chars})" }
my @wolstenholme = lazy ([\+] (1..∞).hyper.map: {FatRat.new: 1, .²}).map: *.numerator;
say 'Wolstenholme numbers:';
printf "%8s: %s\n", .&ordinal-digit(:c), abbr @wolstenholme[$_-1] for flat 1..20, 5e2, 1e3, 2.5e3, 5e3, 1e4;
say "\nPrime Wolstenholme numbers:";
printf "%8s: %s\n", .&ordinal-digit(:c), @wolstenholme.grep(&is-prime)[$_-1]».&abbr for 1..15;
- Output:
Wolstenholme numbers: 1st: 1 2nd: 5 3rd: 49 4th: 205 5th: 5269 6th: 5369 7th: 266681 8th: 1077749 9th: 9778141 10th: 1968329 11th: 239437889 12th: 240505109 13th: 40799043101 14th: 40931552621 15th: 205234915681 16th: 822968714749 17th: 238357395880861 18th: 238820721143261 19th: 86364397717734821 20th: 17299975731542641 500th: 40989667509417020364..48084984597965892703 (digits: 434) 1,000th: 83545938483149689478..99094240207812766449 (digits: 866) 2,500th: 64537911900230612090..12785535902976933153 (digits: 2164) 5,000th: 34472086597488537716..22525144829082590451 (digits: 4340) 10,000th: 54714423173933343999..49175649667700005717 (digits: 8693) Prime Wolstenholme numbers: 1st: 5 2nd: 266681 3rd: 40799043101 4th: 86364397717734821 5th: 36190908596780862323..79995976006474252029 (digits: 104) 6th: 33427988094524601237..48446489305085140033 (digits: 156) 7th: 22812704758392002353..84405125167217413149 (digits: 218) 8th: 28347687473208792918..45794572911130248059 (digits: 318) 9th: 78440559440644426017..30422337523878698419 (digits: 520) 10th: 22706893975121925531..02173859396183964989 (digits: 649) 11th: 27310394808585898968..86311385662644388271 (digits: 935) 12th: 13001072736642048751..08635832246554146071 (digits: 984) 13th: 15086863305391456002..05367804007944918693 (digits: 1202) 14th: 23541935187269979100..02324742766220468879 (digits: 1518) 15th: 40306783143871607599..58901192511859288941 (digits: 1539)