| Bytes | Lang | Time | Link |
|---|---|---|---|
| 063 | APLNARS | 250723T135900Z | Rosario |
| 110 | Python 3 | 230822T055305Z | Rhaixer |
| 007 | Pyt | 240505T141924Z | Kip the |
| 047 | GAWK | 230822T100214Z | Marius_C |
| 009 | Japt v2.0a0 x | 231101T102610Z | Shaggy |
| 4332 | Wolfram Language Mathematica | 230822T081615Z | 138 Aspe |
| 4838 | Pari/GP | 230822T063419Z | 138 Aspe |
| 020 | sclin | 230822T153441Z | Mama Fun |
| 007 | Husk | 230822T132848Z | Dominic |
| 4375 | Vyxal s | 230822T051811Z | lyxal |
| 056 | JavaScript ES6 | 230822T095540Z | Arnauld |
| 007 | Thunno 2 + | 230822T063934Z | The Thon |
| nan | Ruby rprime | 230822T083917Z | G B |
| 032 | Charcoal | 230822T082212Z | Neil |
| 127 | Excel | 230822T060610Z | Jos Wool |
| 071 | Python | 230822T054332Z | bsoelch |
| 041 | Raku | 230822T080248Z | Sean |
| 005 | 05AB1E | 230822T075224Z | Kevin Cr |
| 069 | Desmos | 230822T073004Z | Aiden Ch |
| 035 | Factor + math.primes math.unicode | 230822T062804Z | chunes |
| 066 | Python 3 | 230822T061543Z | xnor |
APL(NARS), 63 chars
r←f w;j
w+←1⋄r←⍬⋄j←w÷w
→3×⍳w≤0⋄j←1πj⋄r,←j⋄w-←1⋄→2
r←+/2{⍺÷⍵}/r
//8+15+27+13= 63 If the input is a rational or big float "v", the output is a rational, if the input is float the output is a float.
f 3x
208r105
f 3
1.980952381
f 7
5.122912588
f 185
179.1897451
f 185x
47752148506472867074638763838344811754955279237362380323460936336224283840376019837825788
08644660355428679348283266958544172716221681797357847578096268781718236396122190592
72221718819558098038143436234124173850168099116519092309146927965885314796198028165
42774045718728603327915977708384371271480484970717073697504033148775214336339953864
14758567792291033680198127968054539321736033209795196804723888951997341145221866183
936637544019198370699032428211686155281139619642036r2664892930881166271482866959666
54714656604183482917950495618298572067762686515928942320813893151097659932181197909
52088310711340165562346919693142105436178412616910243993889870315190322853164714150
83367576874197592494003887711107064408842143573929306097378545964636245380168828046
20546522158807976979978615658815800867141043294274946174853915067303211860210164183
52855233501699608715735469759081421130149548060687058734585470049027535356177084511
745793717898532350701335
Python 3, 131 112 110 bytes
-19 thanks to bsoelch
-2 due to a missed golf
lambda n:sum(x[k]/x[k+1]for k in range(n))
x=[i for i in range(2,7920)if not[i%j for j in range(2,i)if i%j<1]]
Old explanation:
lambda n: # lambda with an argument
x:=[i for i in range(2,7920)if not[i%j for j in range(2,i)if not i%j]][:n+1]
# prime getter till 1000. 7919 is 1000th prime, slice until n+1
sum(x[k]/x[k+1]for k in range(len(x)-1)) # sum it up
[1] # and return the latter
Pyt, 7 bytes
⁺řᵽᵮʁ/Ʃ
⁺ implicit input; increment
ř řangify (1...n+1)
ᵽ get the kth ᵽrime number
ᵮ convert to ᵮloat
ʁ/ ʁeduce the array by division (each element divided by the next)
Ʃ Ʃum; implicit print
GAWK, 49 47 bytes
func f(n){t=0;for(i=1;i<=n;)t+=$i/$++i;print t}
- -2 bytes, thanks to @ceilingcat :
$(++i)to$++i
This one implies that the input file is an infinite list of prime number. The function is still taking an integer n as argument.
As mentionned by @Olivier Dulac, this script need to run using gawk or other awk variant that support having multiple digits fields.
He also suggested a variant that could work with Unix Shell, that would be 61 bytes, and would take as input file an infinite list of prime number, with one number per record :
# HEADER
BEGIN{OFMT = "%.6f"} # Get precision set up
#HEADER
{a[NR]=$0}func f(n){t=0;for(i=1;i<=n;)t+=a[i]/a[++i];print t}
# FOOTER
END{
f(3)
f(185)
f(564)
f(849)
f(999)
}
# FOOTER
AWK, 120 111 109 bytes
{for(a[n=1]=2;t=j=n<1e3;a[++n]=m)for(m=a[n]+(i=1);++i<m;)i=m%i<1?2+0*m++:i;for(;j<$0;)t+=a[++j]/a[1+j];$1=t}1
- -9 bytes, thanks to @ceilingcat
- -2 bytes, replaced
print twith$1=t, then1as precision is not necessary
This one work with just n as input. It only supplies 999 pair of prime number, but if you change 1e3 with 1e4 it will supplies 9999, it will just get 10 time longer for each lines.
Wolfram Language (Mathematica), 43 32 bytes
Saved 11 bytes thanks to the comment of @Roman
N@Sum[Prime@i/Prime[i+1],{i,#}]&
Pari/GP, 48 38 bytes
Saved 10 bytes thanks to the comment of @alephalpha
f(n)=sum(k=1,n,prime(k)/prime(k+1))*1.
sclin, 20 bytes
$P"1dp"Q / rev tk +/
For testing purposes:
999 ; 20N>d
$P"1dp"Q / rev tk +/
NOTE: the default representation is a rational number, but 20N>d converts the representation to decimal for convenience.
Explanation
Prettified code:
$P 1.dp Q / rev tk +/
Assuming input n:
$Pinfinite list of primes1.dp Qduplicate and drop first item/element-wise dividerev tk +/take n elements and sum
Husk, 8 7 bytes
Σ↑Ẋ`/İp
Σ↑Ẋ`/İp
Ẋ # map over adjacent pairs of values
`/ # flipped division: x ÷ y
İp # apply this to the infinite list of primes
↑ # then take the first arg1 values
Σ # and sum them
Vyxal s, 35 bitsv2, 4.375 bytes
ʀǎ¨p/
Works for any n >= 1 (even bigger than 1000). The flag is for display purposes.
Explained
ʀǎ¨p/
ʀǎ # First n prime numbers
¨p/ # With every overlapping pair reduced by divison
# Automatically sum
💎
Created with the help of Luminespire.
Thunno 2 +, 7 bytes
ÆpDḣỊØ.
2 flags -> 1 flag thanks to Neil
Explanation
ÆpDḣỊØ. # Implicit input
Æp # First (input + 1) primes
Dḣ # Push a copy without the first item
Ị # Reciprocal of each
Ø. # Dot product of the two lists
# Implicit output
Charcoal, 32 bytes
Nθ→→W¬›Lυθ¿⬤υ﹪ⅈκ⊞υⅈM→IΣEΦυκ∕§υκι
Try it online! Link is to verbose version of code. Explanation:
Nθ→→W¬›Lυθ¿⬤υ﹪ⅈκ⊞υⅈM→
Input n and generate the first n+1 primes using the same method as for my answer to Number of bits needed to represent the product of the first primes.
IΣEΦυκ∕§υκι
Divide each prime by its successor and output the sum.
30 bytes using the newer version of Charcoal on ATO:
Nθ→→W¬›Lυθ¿⬤υ﹪ⅈκ⊞υⅈM→IΣ×υ∕¹Φυκ
Attempt This Online! Link is to verbose version of code. Explanation: Takes the dot product of the primes with the reciprocals of the odd primes.
Excel, 127 bytes
=LET(
a,ROW(1:7327),
b,FILTER(a,MMULT(N(MOD(a,TOROW(a))=0),a^0)=2),
SUM(EXP(MMULT(LN(INDEX(b,SEQUENCE(A1)+{0,1})^{1,-1}),{1;1})))
)
Input in cell A1. Fails for A1>932.
Explanation
ROW(1:7327)
# Vertical array of integers 1 to 7327
TOROW(a)
# Transpose the above
MOD(a,TOROW(a))
# 2D array of all those integers modulo all those integers
MOD(a,TOROW(a))=0)
# Which of these is equal to 0
N(MOD(a,TOROW(a))=0)
# Convert Booleans to ones and zeroes
a^0
# Vertical array of size 7327, each entry unity
MMULT(N(MOD(a,TOROW(a))=0),a^0)
# Matrix multiplication of the above arrays
MMULT(N(MOD(a,TOROW(a))=0),a^0)=2
# Which of these is equal to 2, i.e. only divisors are unity and itself
FILTER(a,MMULT(N(MOD(a,TOROW(a))=0),a^0)=2)
# Filter array of integers accordingly, i.e. list of primes <=7327
SEQUENCE(A1)
# Vertical array from 1 to value in cell A1
SEQUENCE(A1)+{0,1}
# Additional column added to above with values offset by 1
INDEX(b,SEQUENCE(A1)+{0,1})
# Index list of primes with these indices
INDEX(b,SEQUENCE(A1)+{0,1})^{1,-1}
# Reciprocate the last entry in each pair with unity
LN(INDEX(b,SEQUENCE(A1)+{0,1})^{1,-1})
# Take the natural logarithm
MMULT(LN(INDEX(b,SEQUENCE(A1)+{0,1})^{1,-1}),{1;1})
# Matrix multiplication for pairwise summation
EXP(MMULT(LN(INDEX(b,SEQUENCE(A1)+{0,1})^{1,-1}),{1;1}))
# Take the natural exponentiation
SUM(EXP(MMULT(LN(INDEX(b,SEQUENCE(A1)+{0,1})^{1,-1}),{1;1})))
# Sum all entries
Python, 71 bytes
lambda n:sum(p(k+1)/p(k+2)for k in range(n))
from sympy import*
p=prime
Explanation
prime computes the n-th prime
Python, 105 bytes
longer but faster
lambda n:sum(a/b for(a,b)in i.pairwise(i.islice(sympy.primerange(4**n),n+1)))
import sympy,itertools as i
Explanation
Uses the fact that the n+1-st prime is less than 4n+1 to obtain a list of the first n+1 primes from the list of the primes less than n (sympy.primerange).
pairwise groups the elements in adjacent pairs
Raku, 41 bytes
[\+] {.(2)Z/.(3)}({grep &is-prime,$_..*})
This is an expression for the infinite sequence of partial sums.
{ grep &is-prime, $_ .. * }is an anonymous function that returns a sequence of primes starting from the number given as its argument.{ .(2) Z/ .(3) }is another anonymous function that is given the previous anonymous function as its argument in the variable$_, and evaluated immediately..(2)calls the first function with the number 2, generating the primes starting from 2, and likewise with.(3).Z/zips those sequences together with division, producing the sequence of fractions.[\+]produces the infinite sequence of partial sums.
05AB1E, 6 5 bytes
ÝØü/O
Try it online or verify all test cases.
Explanation:
Ý # Push a list in the range [0, (implicit) input]
Ø # Convert each to their 0-based n'th prime
ü # For each overlapping pair of values:
/ # Divide them
O # Them sum all these decimal values together
# (which is output implicitly as result)
Desmos, 69 bytes
L=[2...7920]
P=L[∏_{d=3}^Lmod(L,d-1)>0]
f(k)=∑_{n=1}^kP[n]/P[n+1]
Try It On Desmos! - Prettified
Probably golfable but this is the best I could think of for now.
Factor + math.primes math.unicode, 35 bytes
[ 1 + nprimes 2 clump unzip v/ Σ ]
! 3
1 ! 3 1
+ ! 4
nprimes ! { 2 3 5 7 }
2 ! { 2 3 5 7 } 2
clump ! { { 2 3 } { 3 5 } { 5 7 } }
unzip ! { 2 3 5 } { 3 5 7 }
v/ ! { 2/3 3/5 5/7 }
Σ ! 1+103/105
Python 3, 66 bytes
f=lambda n,p=3,P=4,d=2:n and P%p*d/p+f(n-P%p,p+1,P*p*p,[d,p][P%p])
The Wilson's Theorem generator strikes again. Similar to the template
for the first n primes here, but also stores the previous prime as d and keeps the running sum of the ratios.