Prime reciprocal sum

From Rosetta Code
Revision as of 01:45, 12 May 2023 by Rdm (talk | contribs) (J)
Prime reciprocal sum 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.

Generate the sequence of primes where each term is the smallest prime whose reciprocal can be added to the cumulative sum and remain smaller than 1.


E.G.
The cumulative reciprocal sum with zero terms is 0. The smallest prime whose reciprocal can be added to the sum without reaching or exceeding 1 is 2, so the first term is 2 and the new cumulative reciprocal sum is 1/2.
The smallest prime whose reciprocal can be added to the sum without reaching or exceeding 1 is 3. (2 would cause the cumulative reciprocal sum to reach 1.) So the next term is 3, and the new cumulative sum is 5/6.
and so on...


Task
  • Find and display the first 10 terms of the sequence. (Or as many as reasonably supported by your language if it is less.) For any values with more than 40 digits, show the first and last 20 digits and the overall digit count.


Stretch
  • Find and display the next 5 terms of the sequence. (Or as many as you have the patience for.) Show only the first and last 20 digits and the overall digit count.


See also



J

Given:

taskfmt=: {{
  if. 40<#t=. ":y do.
    d=. ":#t
    (20{.t),'..',(_20{.t),' (',d,' digits)'
  else.
    t
  end.
}}@>

The task and stretch could be:

   taskfmt (, 4 p:1%1-+/@:%)^:(15)i.0
2                                                       
3                                                       
7                                                       
43                                                      
1811                                                    
654149                                                  
27082315109                                             
153694141992520880899                                   
337110658273917297268061074384231117039                 
84241975970641143191..13803869133407474043 (76 digits)  
20300753813848234767..91313959045797597991 (150 digits) 
20323705381471272842..21649394434192763213 (297 digits) 
12748246592672078196..20708715953110886963 (592 digits) 
46749025165138838243..65355869250350888941 (1180 digits)
11390125639471674628..31060548964273180103 (2358 digits)

Raku

The sixteenth is very slow to emerge. Didn't bother trying for the seventeenth. OEIS only lists the first fourteen values.

sub abbr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '..' ~ .substr(*-20) ~ " ({.chars} digits)" }

sub next-prime {
    state $sum = 0;
    my $next = ((1 / (1 - $sum)).ceiling+1..*).hyper(:2batch).grep(&is-prime)[0];
    $sum += FatRat.new(1,$next);
    $next;
}

printf "%2d: %s\n", $_, abbr next-prime for 1..15;
Output:
 1: 2
 2: 3
 3: 7
 4: 43
 5: 1811
 6: 654149
 7: 27082315109
 8: 153694141992520880899
 9: 337110658273917297268061074384231117039
10: 84241975970641143191..13803869133407474043 (76 digits)
11: 20300753813848234767..91313959045797597991 (150 digits)
12: 20323705381471272842..21649394434192763213 (297 digits)
13: 12748246592672078196..20708715953110886963 (592 digits)
14: 46749025165138838243..65355869250350888941 (1180 digits)
15: 11390125639471674628..31060548964273180103 (2358 digits)
16: 36961763505630520555..02467094377885929191 (4711 digits)