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 265 characters in body
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Haskell, 13 bytes

This is the shortest (I (削除) think (削除ここまで)thought):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree direct, 15 bytes

(*)=<<(/2).(+1)

Thanks @nimi for the idea!

Try it online!

Pointfree via sum, 16 bytes

sum.enumFromTo 1

Try it online!

Recursively, (削除) 22 (削除ここまで) 18 bytes

f 0=0;f n=n+f(n-1)

Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

Haskell, 13 bytes

This is the shortest (I (削除) think (削除ここまで)thought):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Recursively, (削除) 22 (削除ここまで) 18 bytes

f 0=0;f n=n+f(n-1)

Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

Haskell, 13 bytes

This is the shortest (I (削除) think (削除ここまで)thought):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree direct, 15 bytes

(*)=<<(/2).(+1)

Thanks @nimi for the idea!

Try it online!

Pointfree via sum, 16 bytes

sum.enumFromTo 1

Try it online!

Recursively, (削除) 22 (削除ここまで) 18 bytes

f 0=0;f n=n+f(n-1)

Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

.
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Haskell, 13 bytes

This is the shortest (I think(削除) think (削除ここまで)thought):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Recursively, (削除) 22 (削除ここまで) 18 bytes

f 0=0;f n=n+f(n-1)

Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

Haskell, 13 bytes

This is the shortest (I think):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Recursively, (削除) 22 (削除ここまで) 18 bytes

f 0=0;f n=n+f(n-1)

Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

Haskell, 13 bytes

This is the shortest (I (削除) think (削除ここまで)thought):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Recursively, (削除) 22 (削除ここまで) 18 bytes

f 0=0;f n=n+f(n-1)

Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

-4 bytes.
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Haskell, 13 bytes

This is the shortest (I think):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Standard foldRecursively, 19(削除) 22 (削除ここまで) 18 bytes

f n=foldr0=0;f n=n+f(+n-1)0[1..n]

Try it online! Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

RecursivelyStandard fold, 2219 bytes

f n|n<1=0|1<2=n+fn=foldr(n-1+)0[1..n]

Thanks @maple_shaft for the idea!

Try it online! Try it online!

Haskell, 13 bytes

This is the shortest (I think):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

Recursively, 22 bytes

f n|n<1=0|1<2=n+f(n-1)

Thanks @maple_shaft for the idea!

Try it online!

Haskell, 13 bytes

This is the shortest (I think):

f n=sum[1..n]

Try it online!

Direct, (削除) 17 (削除ここまで) 13 bytes

f n=n*(n+1)/2

Thanks @WheatWizard for -4 bytes!

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Recursively, (削除) 22 (削除ここまで) 18 bytes

f 0=0;f n=n+f(n-1)

Thanks @maple_shaft for the idea & @Laikoni for golfing it!

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

Added another one.
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106
Loading
-4 bytes.
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106
Loading
Added other methods..
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106
Loading
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106
Loading

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