Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 3 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 16 bytes

liqZqtd2=)t2+h/s

Try it online!

Consider input 13 as an example.

l % Push 1
 % STACK: 1
i % Input N
 % STACK: 1, 13
q % Subtract 1
 % STACK: 1, 12
Zq % Primes up to Nthat
 % STACK: 1, [2 3 5 7 11]
t % Duplicate
 % STACK: 1, [2 3 5 7 11], [2 3 5 7 11]
d % Consecutive differences
 % STACK: 1, [2 3 5 7 11], [1 2 2 4]
2= % Compare with 2, element-wise
 % STACK: 1, [2 3 5 7 11], [0 1 1 0]
) % Use as logical index to select elements from array
 % STACK: 1, [3 5]
t % Duplicate
 % STACK: 1, [3 5], [3 5]
2+ % Add 2, element-wise
 % STACK: 1, [3 5], [5 7]
h % Concatenate horizontally
 % STACK: 1, [3 5 5 7]
/ % Divide, element-wise
 % STACK: [0.3333 0.2 0.2 0.1429]
s % Sum of array. Implicitly display
 % STACK: 0.8762

MATL, 16 bytes

liqZqtd2=)t2+h/s

Try it online!

Consider input 13 as an example.

l % Push 1
 % STACK: 1
i % Input N
 % STACK: 1, 13
q % Subtract 1
 % STACK: 1, 12
Zq % Primes up to N
 % STACK: 1, [2 3 5 7 11]
t % Duplicate
 % STACK: 1, [2 3 5 7 11], [2 3 5 7 11]
d % Consecutive differences
 % STACK: 1, [2 3 5 7 11], [1 2 2 4]
2= % Compare with 2, element-wise
 % STACK: 1, [2 3 5 7 11], [0 1 1 0]
) % Use as logical index to select elements from array
 % STACK: 1, [3 5]
t % Duplicate
 % STACK: 1, [3 5], [3 5]
2+ % Add 2, element-wise
 % STACK: 1, [3 5], [5 7]
h % Concatenate horizontally
 % STACK: 1, [3 5 5 7]
/ % Divide, element-wise
 % STACK: [0.3333 0.2 0.2 0.1429]
s % Sum of array. Implicitly display
 % STACK: 0.8762

MATL, 16 bytes

liqZqtd2=)t2+h/s

Try it online!

Consider input 13 as an example.

l % Push 1
 % STACK: 1
i % Input N
 % STACK: 1, 13
q % Subtract 1
 % STACK: 1, 12
Zq % Primes up to that
 % STACK: 1, [2 3 5 7 11]
t % Duplicate
 % STACK: 1, [2 3 5 7 11], [2 3 5 7 11]
d % Consecutive differences
 % STACK: 1, [2 3 5 7 11], [1 2 2 4]
2= % Compare with 2, element-wise
 % STACK: 1, [2 3 5 7 11], [0 1 1 0]
) % Use as logical index to select elements from array
 % STACK: 1, [3 5]
t % Duplicate
 % STACK: 1, [3 5], [3 5]
2+ % Add 2, element-wise
 % STACK: 1, [3 5], [5 7]
h % Concatenate horizontally
 % STACK: 1, [3 5 5 7]
/ % Divide, element-wise
 % STACK: [0.3333 0.2 0.2 0.1429]
s % Sum of array. Implicitly display
 % STACK: 0.8762
Post Undeleted by Luis Mendo
added 1 character in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 1516 bytes

liZqtd2=liqZqtd2=)t2+h/s

Try it online!

Consider input 13 as an example.

l % Push 1
 % STACK: 1
i % Input N
 % STACK: 1, 13
q % Subtract 1
 % STACK: 1, 12
Zq % Primes up to N
 % STACK: 1, [2 3 5 7 11]
t % Duplicate
 % STACK: 1, [2 3 5 7 11], [2 3 5 7 11]
d % Consecutive differences
 % STACK: 1, [2 3 5 7 11], [1 2 2 4]
2= % Compare with 2, element-wise
 % STACK: 1, [2 3 5 7 11], [0 1 1 0]
) % Use as logical index to select elements from array
 % STACK: 1, [3 5]
t % Duplicate
 % STACK: 1, [3 5], [3 5]
2+ % Add 2, element-wise
 % STACK: 1, [3 5], [5 7]
h % Concatenate horizontally
 % STACK: 1, [3 5 5 7]
/ % Divide, element-wise
 % STACK: [0.3333 0.2 0.2 0.1429]
s % Sum of array. Implicitly display
 % STACK: 0.8762

MATL, 15 bytes

liZqtd2=)t2+h/s

Try it online!

MATL, 16 bytes

liqZqtd2=)t2+h/s

Try it online!

Consider input 13 as an example.

l % Push 1
 % STACK: 1
i % Input N
 % STACK: 1, 13
q % Subtract 1
 % STACK: 1, 12
Zq % Primes up to N
 % STACK: 1, [2 3 5 7 11]
t % Duplicate
 % STACK: 1, [2 3 5 7 11], [2 3 5 7 11]
d % Consecutive differences
 % STACK: 1, [2 3 5 7 11], [1 2 2 4]
2= % Compare with 2, element-wise
 % STACK: 1, [2 3 5 7 11], [0 1 1 0]
) % Use as logical index to select elements from array
 % STACK: 1, [3 5]
t % Duplicate
 % STACK: 1, [3 5], [3 5]
2+ % Add 2, element-wise
 % STACK: 1, [3 5], [5 7]
h % Concatenate horizontally
 % STACK: 1, [3 5 5 7]
/ % Divide, element-wise
 % STACK: [0.3333 0.2 0.2 0.1429]
s % Sum of array. Implicitly display
 % STACK: 0.8762
Post Deleted by Luis Mendo
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 15 bytes

liZqtd2=)t2+h/s

Try it online!

AltStyle によって変換されたページ (->オリジナル) /