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 expl.
Source Link
wastl
  • 4.4k
  • 16
  • 35

Stax, 11 bytes

ó╢Δ'åç4}ú┌7

Run and debug it

Explanation:

We want to calculate:

$$\sum_{i=1}^n{\frac 1 i}$$

We now need a denominator \$b\$ and a list of numerators \$a_i\$:

$$\sum_{i=1}^n{\frac{a_i}b}=\frac{\sum_{i=1}^n{a_i}}{b}$$

We can make \$b=n!\$, then we have:

\begin{align} \frac{a_i}{n!}&=\frac 1 i&|\times n! \\ a_i&=\frac{n!}i \end{align}

So we have:

$$\sum_{i=1}^n{\frac 1 n}=\frac{\sum_{i=1}^n{\frac{n!}i}}{n!}$$

|Fx{[/m|+L:_m Full program
|F Factorial
 x Push input again
 { m Map over range [1, n]
 [ Copy the factorial
 / Divide factorial by current value
 |+ Sum
 L Listify stack, top gets first element
 :_ Divide both values by gcd
 m Print each followed by newline

Stax, 11 bytes

ó╢Δ'åç4}ú┌7

Run and debug it

Stax, 11 bytes

ó╢Δ'åç4}ú┌7

Run and debug it

Explanation:

We want to calculate:

$$\sum_{i=1}^n{\frac 1 i}$$

We now need a denominator \$b\$ and a list of numerators \$a_i\$:

$$\sum_{i=1}^n{\frac{a_i}b}=\frac{\sum_{i=1}^n{a_i}}{b}$$

We can make \$b=n!\$, then we have:

\begin{align} \frac{a_i}{n!}&=\frac 1 i&|\times n! \\ a_i&=\frac{n!}i \end{align}

So we have:

$$\sum_{i=1}^n{\frac 1 n}=\frac{\sum_{i=1}^n{\frac{n!}i}}{n!}$$

|Fx{[/m|+L:_m Full program
|F Factorial
 x Push input again
 { m Map over range [1, n]
 [ Copy the factorial
 / Divide factorial by current value
 |+ Sum
 L Listify stack, top gets first element
 :_ Divide both values by gcd
 m Print each followed by newline
Source Link
wastl
  • 4.4k
  • 16
  • 35

Stax, 11 bytes

ó╢Δ'åç4}ú┌7

Run and debug it

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