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

Commonmark migration
Source Link

#Octave, 45 bytes

Octave, 45 bytes

@(n)sum(all(isprime(m=[h=3:n-1;h-2]))*m'.^-1)

Explanation:

m=[h=3:n-1;h-2] generate an concatenate two ranges 3:n-1 and 1:n-3
rec=m'.^-1 transpose and reciprocal
idx=all(isprime(m)) create a logical [0 1 ..] array if both ranges are prime set 1 else set 0
sum1 = idx * rec matrix multiplication(extrat elements with logical index and sum along the first dimension)
sum(sum1) sum along the second dimension 

Try It Online!

#Octave, 45 bytes

@(n)sum(all(isprime(m=[h=3:n-1;h-2]))*m'.^-1)

Explanation:

m=[h=3:n-1;h-2] generate an concatenate two ranges 3:n-1 and 1:n-3
rec=m'.^-1 transpose and reciprocal
idx=all(isprime(m)) create a logical [0 1 ..] array if both ranges are prime set 1 else set 0
sum1 = idx * rec matrix multiplication(extrat elements with logical index and sum along the first dimension)
sum(sum1) sum along the second dimension 

Try It Online!

Octave, 45 bytes

@(n)sum(all(isprime(m=[h=3:n-1;h-2]))*m'.^-1)

Explanation:

m=[h=3:n-1;h-2] generate an concatenate two ranges 3:n-1 and 1:n-3
rec=m'.^-1 transpose and reciprocal
idx=all(isprime(m)) create a logical [0 1 ..] array if both ranges are prime set 1 else set 0
sum1 = idx * rec matrix multiplication(extrat elements with logical index and sum along the first dimension)
sum(sum1) sum along the second dimension 

Try It Online!

Source Link
rahnema1
  • 5.7k
  • 1
  • 16
  • 22

#Octave, 45 bytes

@(n)sum(all(isprime(m=[h=3:n-1;h-2]))*m'.^-1)

Explanation:

m=[h=3:n-1;h-2] generate an concatenate two ranges 3:n-1 and 1:n-3
rec=m'.^-1 transpose and reciprocal
idx=all(isprime(m)) create a logical [0 1 ..] array if both ranges are prime set 1 else set 0
sum1 = idx * rec matrix multiplication(extrat elements with logical index and sum along the first dimension)
sum(sum1) sum along the second dimension 

Try It Online!

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