Skip to main content
Better version of the code.
Source Link
cnamejj
  • 2.9k
  • 10
  • 11

Ly, 1414 12 bytes

0ns[1fn[:1f/+l1f1-s]p]&+ 

Try it online!Try it online!

Explaining Ly is tricky for me, but here goesFound a simpler way to do this... The code just keeps the accumulated value and the loop counter on the stack, and using the "backup cell" in Ly to avoid having to duplicate the loop counter before doing theoriginal was: 10ns[1f/n+l1-s]p math

And here's what it's doing.

0nsn 

ReadGet 'n' from the challenge (the number of iterations) from the input, then copies it to the backup cell with and push onto the s commandstack.

[...] 

Loops asAs long as the top of the stack (the decrementing loop counter) is not 0non-zero.

:1f/+ 

CalculatesDuplicate the top of the stack (the iteration counter), and compute 1/n where n it the loop counter (top of the stack). Then adds that to the previous value of the accumulator.

l1f1-s 

Pulls the loop counter from the backup cell, decrements it and saves it back. AfterwardsFlip the top oftwo entries on the stack isso that the next value of 1/n or 0calculation is second and the iteration counter is on the top. Then decrement the iteration counter.

p&+ 

When the loop,Once all that's left to do it pop the 0 loop counter ofvalues have been computed, sum the stack and the accumulated value will be printedprint.

Ly, 14 bytes

0ns[1f/+l1-s]p 

Try it online!

Explaining Ly is tricky for me, but here goes... The code just keeps the accumulated value and the loop counter on the stack, and using the "backup cell" in Ly to avoid having to duplicate the loop counter before doing the 1/n math.

0ns 

Read the number of iterations from the input, then copies it to the backup cell with the s command.

[...] 

Loops as long as the top of the stack (the decrementing loop counter) is not 0.

1f/+ 

Calculates 1/n where n it the loop counter (top of the stack). Then adds that to the previous value of the accumulator.

l1-s 

Pulls the loop counter from the backup cell, decrements it and saves it back. Afterwards the top of the stack is the next value of n or 0.

p 

When the loop, all that's left to do it pop the 0 loop counter of the stack and the accumulated value will be printed.

Ly, 14 12 bytes

n[:1f/f1-]&+ 

Try it online!

Found a simpler way to do this... The original was: 0ns[1f/+l1-s]p

And here's what it's doing.

n 

Get 'n' from the challenge (the number of iterations) from the input and push onto the stack.

[...] 

As long as the top of the stack is non-zero.

:1f/ 

Duplicate the top of the stack (the iteration counter), and compute 1/n.

f1- 

Flip the two entries on the stack so that the 1/n calculation is second and the iteration counter is on the top. Then decrement the iteration counter.

&+ 

Once all the values have been computed, sum the stack and print.

edited body
Source Link
cnamejj
  • 2.9k
  • 10
  • 11

Ly, 14 bytes

0ns[1f/+l1-s]p 

Try it online!

Explaining Ly is tricky for me, but here goes... The code just keepkeeps the accumulated value and the loop counter on the stack, and using the "backup cell" in Ly to avoid having to duplicate the loop counter before doing the 1/n math.

0ns 

Read the number of iterations from the input, then copies it to the backup cell with the s command.

[...] 

Loops as long as the top of the stack (the decrementing loop counter) is not 0.

1f/+ 

Calculates 1/n where n it the loop counter (top of the stack). Then adds that to the previous value of the accumulator.

l1-s 

Pulls the loop counter from the backup cell, decrements it and saves it back. Afterwards the top of the stack ifis the next value of n or 0.

p 

When the loop, all that's left to do it pop the 0 loop counter of the stack and the accumulated value will be printed.

Ly, 14 bytes

0ns[1f/+l1-s]p 

Try it online!

Explaining Ly is tricky for me, but here goes... The code just keep the accumulated value and the loop counter on the stack, and using the "backup cell" in Ly to avoid having to duplicate the loop counter before doing the 1/n math.

0ns 

Read the number of iterations from the input, then copies it to the backup cell with the s command.

[...] 

Loops as long as the top of the stack (the decrementing loop counter) is not 0.

1f/+ 

Calculates 1/n where n it the loop counter (top of the stack). Then adds that to the previous value of the accumulator.

l1-s 

Pulls the loop counter from the backup cell, decrements it and saves it back. Afterwards the top of the stack if the next value of n or 0.

p 

When the loop, all that's left to do it pop the 0 loop counter of the stack and the accumulated value will be printed.

Ly, 14 bytes

0ns[1f/+l1-s]p 

Try it online!

Explaining Ly is tricky for me, but here goes... The code just keeps the accumulated value and the loop counter on the stack, and using the "backup cell" in Ly to avoid having to duplicate the loop counter before doing the 1/n math.

0ns 

Read the number of iterations from the input, then copies it to the backup cell with the s command.

[...] 

Loops as long as the top of the stack (the decrementing loop counter) is not 0.

1f/+ 

Calculates 1/n where n it the loop counter (top of the stack). Then adds that to the previous value of the accumulator.

l1-s 

Pulls the loop counter from the backup cell, decrements it and saves it back. Afterwards the top of the stack is the next value of n or 0.

p 

When the loop, all that's left to do it pop the 0 loop counter of the stack and the accumulated value will be printed.

Source Link
cnamejj
  • 2.9k
  • 10
  • 11

Ly, 14 bytes

0ns[1f/+l1-s]p 

Try it online!

Explaining Ly is tricky for me, but here goes... The code just keep the accumulated value and the loop counter on the stack, and using the "backup cell" in Ly to avoid having to duplicate the loop counter before doing the 1/n math.

0ns 

Read the number of iterations from the input, then copies it to the backup cell with the s command.

[...] 

Loops as long as the top of the stack (the decrementing loop counter) is not 0.

1f/+ 

Calculates 1/n where n it the loop counter (top of the stack). Then adds that to the previous value of the accumulator.

l1-s 

Pulls the loop counter from the backup cell, decrements it and saves it back. Afterwards the top of the stack if the next value of n or 0.

p 

When the loop, all that's left to do it pop the 0 loop counter of the stack and the accumulated value will be printed.