├── APL.apl ├── Agda.agda ├── Basic.bas ├── Brainfuck.bf ├── C Sharp.cs ├── C++.cpp ├── C.c ├── Common Lisp.lisp ├── Cuda.cu ├── D.d ├── Dart.dart ├── Erlang.erl ├── Fortran.f90 ├── Futhark.fut ├── Go.go ├── Haskell.hs ├── Java.java ├── JavaScript.js ├── Julia.jl ├── Koltlin.kt ├── Mathematica.m ├── Maxima.max ├── PHP.php ├── Pascal.pas ├── Perl.pl ├── Python.py ├── R.r ├── README.md ├── Racket.rkt ├── Ruby.rb ├── Rust.rs ├── SML.sml ├── Scala.scala ├── Shell.sh ├── Smalltalk.st ├── Swift.swift ├── TypeScript.ts ├── UNLICENSE └── Zig.zig /APL.apl: -------------------------------------------------------------------------------- 1 | ⎕ ← (({⍵+1 (⊃1↑⍵)} ⍣ 1000000000) 1 0)[2] 2 | -------------------------------------------------------------------------------- /Agda.agda: -------------------------------------------------------------------------------- 1 | open import Data.Nat using (ℕ; zero; suc; _+_) 2 | open import Data.Nat.Show using (show) 3 | open import Function using (_$!_) 4 | open import IO using (run; putStrLn) 5 | 6 | sum : ℕ 7 | sum = f 1000000000 0 8 | where 9 | f : ℕ → ℕ → ℕ 10 | f zero acc = acc 11 | f (suc n) acc = f n $! suc n + acc 12 | 13 | main = run (putStrLn (show sum)) 14 | -------------------------------------------------------------------------------- /Basic.bas: -------------------------------------------------------------------------------- 1 | sum = 0 2 | for i = 1 to 1000000000 3 | sum = sum + i 4 | next i 5 | print sum 6 | -------------------------------------------------------------------------------- /Brainfuck.bf: -------------------------------------------------------------------------------- 1 | [ This is the specification ... 2 | 3 | #include 4 | int main() 5 | { 6 | long long sum = 0; 7 | int i; 8 | for (i = 1; i <= 1000000000; i++) 9 | sum += i; 10 | printf("%lld\n", sum); 11 | } 12 | 13 | So I need the variable "I" capable of storing a 10 digit number. 14 | I need "sum" capable of storing a 20 digit number. 15 | I need a comparison with 1000000000 or a loop that can run a counted 16 | 1000000000 times. 17 | The I need to print out the generated number. 18 | 19 | Inc operation. 20 | Carry in Add to cell, if cell is 10 zero it and move right. 21 | repeat 22 | Run back to start. 23 | 24 | Add operation. 25 | Add Carry + Cell+2n to Cell, repeat until no carry nor numbers. 26 | 27 | Run back to start. 28 | 29 | Print opration. 30 | Run to end of number, print in reverse. 31 | 32 | $ tritium -b triangle.b 33 | Total nodes 160, loaded 394 (24k) 34 | Offset range 0..24, T_MOV range -7..7, Minimum MAAD offset -5 35 | Tokens: T_MOV=6, T_ADD=14, T_PRT=2, T_WHL=16, T_END=16, T_SET=35, T_CALC=8 36 | ... T_CHR=53, T_IF=5, T_ENDIF=5 37 | Running tree using 'dynasm' generator 38 | Compiled 805 bytes of i686 Dynasm code, running. 39 | Calculating sum of integers from 1 to 1000000000 ... 500000000500000000 40 | Run time 586.498214s, I/O time 0.000073s 41 | $ 42 | ] 43 | Title 44 | [-]>[-]++++++++[<++++++++>-]<+++.>+++++[<++++++>-]<.+++++++++++.---------.>+ 45 | +++[<++++>-]<++.---------.-----------.>++++[<++++>-]<+++.-----------.+++++.- 46 | ------.[-]>+++++[<++++++>-]<++.>+++++++++[<+++++++++>-]<++.++.--------.[-]>+ 47 | ++++[<++++++>-]<++.>++++++++[<+++++++++>-]<+++++++.---------.[-]>+++++[<++++ 48 | ++>-]<++.>++++++++[<+++++++++>-]<+.+++++.++++++.---------------.++.--.++++++ 49 | +++++++.+.[-]>+++++[<++++++>-]<++.>++++++++[<++++++++>-]<++++++.++++++++++++ 50 | .---.--.[-]>+++++[<++++++>-]<++.>++++[<++++>-]<+.>++++[<---->-]<-.>+++++++++ 51 | [<+++++++++>-]<+++.-----.[-]>+++++[<++++++>-]<++.>++++[<++++>-]<+.-......... 52 | >++++[<---->-]<.++++++++++++++...--------------.[-] 53 | Loop for 10*10*10*10*10*10*10*10*10 54 | >++++++++++[->++++++++++[->++++++++++[->++++++++++[->++++++++++[->++++++++++ 55 | [->++++++++++[->++++++++++[->++++++++++[- 56 | Increment the "I" value 57 | >>>>>>>>[-]+<[-]+[<<<<+>[-]<[->+>+<<]>>[-<<+>>]+<----------[>>>>>>>>>>[-]<<< 58 | <<<<<<<[-]>[-]<]>[<<[-]>>>>>>>>>>>[-]+<<<<<<<<<[-]]>>>[-]+>>>>>>]<<<<<<[<<<< 59 | <<<] 60 | Add the "I" to the "sum" 61 | >>>>>>[-]>[<[-<<<<<+>>>>>]<<<<[-<+>>+<]>[-<+>]>>[-]<[-]++++++++++<<<[->>+>[- 62 | [->+<]]>[-<+>]<<<<]>>[-<<+>>]>>+<[>>>>>>>>>[-]<<<<<<<<[-]<[-]]>[->>>>>>>>[-] 63 | +<<<<<<<<<<<<---------->>>>>>>>>>>>>[-]+<<<<<<<<<]>>[-]+>>>>>>>]<<<<<<<[<<<< 64 | <<<] 65 | End of the loops 66 | <]<]<]<]<]<]<]<]<] 67 | Finally print out the "sum" 68 | >>>>>>>>>>>>>>>>[>>>>>>>]<<<<<<<[<<<<[-]>[-]<<<[->>+>+<<<]>>>[-<<<+>>>]+++++ 69 | +++[-<++++++>]<.[-]<<<]<<<<<<<<<<++++++++++.[-] 70 | -------------------------------------------------------------------------------- /C Sharp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | 5 | static class SumToBillion 6 | { 7 | static void Main() 8 | { 9 | Console.WriteLine(Range(1000000000L).Sum()); 10 | } 11 | 12 | static IEnumerable Range(long Max) 13 | { 14 | for (long i = 1; i <= Max; i++) 15 | yield return i; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /C++.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long sum = 0; 6 | 7 | int main() 8 | { 9 | for (int i = 1; i <= 1000000000; i++) 10 | sum += i; 11 | cout << sum << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /C.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | long long sum = 0; 5 | int i; 6 | for (i = 1; i <= 1000000000; i++) 7 | sum += i; 8 | printf("%lld\n", sum); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Common Lisp.lisp: -------------------------------------------------------------------------------- 1 | (defparameter *sum* 0) 2 | (loop for i from 1 to 1000000000 do 3 | (incf *sum* i)) 4 | (format t "~d" *sum*) 5 | -------------------------------------------------------------------------------- /Cuda.cu: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | thrust::counting_iterator start(1); 8 | int64_t sum = thrust::reduce(start, 9 | start + 1000000000, 10 | 0, 11 | thrust::plus()); 12 | 13 | std::cout << sum << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /D.d: -------------------------------------------------------------------------------- 1 | import std.stdio; 2 | 3 | void main(){ 4 | double output = 0; 5 | 6 | for(double i = 0; i < 1_000_000_000; i++){ 7 | output += i; 8 | } 9 | writeln(output); 10 | } -------------------------------------------------------------------------------- /Dart.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | var sum = 0; 3 | for (var i = 0; i <= 1000000000; ++i) { 4 | sum += i; 5 | } 6 | print(sum); 7 | } 8 | -------------------------------------------------------------------------------- /Erlang.erl: -------------------------------------------------------------------------------- 1 | -module(sum). 2 | 3 | -export([sum/0]). 4 | -define(BILLION, 1000000000). 5 | 6 | -spec sum(non_neg_integer(), non_neg_integer()) -> non_neg_integer(). 7 | sum(0, Acc) -> 8 | Acc; 9 | sum(N, Acc) -> 10 | sum(N-1, Acc + N). 11 | 12 | -spec sum() -> non_neg_integer(). 13 | sum() -> 14 | sum(?BILLION, 0). 15 | 16 | 17 | -------------------------------------------------------------------------------- /Fortran.f90: -------------------------------------------------------------------------------- 1 | program sum 2 | implicit none 3 | 4 | integer :: i 5 | integer(kind=8) :: s 6 | 7 | s = 0 8 | do i = 1, 1000000000 9 | s = s + i 10 | end do 11 | 12 | write(*,*) 'sum: ', s 13 | 14 | end program sum 15 | 16 | 17 | -------------------------------------------------------------------------------- /Futhark.fut: -------------------------------------------------------------------------------- 1 | fun main(): i64 = 2 | reduce (+) 0i64 (map i64 (map (1+) (iota 1000000000))) -------------------------------------------------------------------------------- /Go.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var sum, i int64 7 | sum = 0 8 | for i = 1; i <= 1000000000; i++ { 9 | sum += i 10 | } 11 | fmt.Printf("%d\n", sum) 12 | } 13 | -------------------------------------------------------------------------------- /Haskell.hs: -------------------------------------------------------------------------------- 1 | import Data.List (foldl') 2 | 3 | main = print $ foldl' (+) (0::Int64) [1..1000000000] 4 | -------------------------------------------------------------------------------- /Java.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.BigInteger; 3 | import java.util.*; 4 | 5 | public class sum 6 | { 7 | public static void main(String args[]) 8 | { 9 | int n = 1000000000; 10 | BigInteger ans = BigInteger.ZERO; 11 | for(int i = 1; i <= n; i++) 12 | ans = ans.add(BigInteger.valueOf(i)); 13 | System.out.println(ans); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /JavaScript.js: -------------------------------------------------------------------------------- 1 | var j = 0n; 2 | 3 | for (i = 1n; i <= 1000000000n; i++) { 4 | j += i; 5 | } 6 | 7 | console.log(j.toString()); 8 | -------------------------------------------------------------------------------- /Julia.jl: -------------------------------------------------------------------------------- 1 | sum = 0 2 | for i in 1:1000000000 3 | sum += i 4 | end 5 | print(sum) 6 | -------------------------------------------------------------------------------- /Koltlin.kt: -------------------------------------------------------------------------------- 1 | package sum 2 | 3 | import java.math.BigInteger; 4 | 5 | fun main(args: Array) { 6 | var ans :BigInteger = BigInteger.ZERO; 7 | 8 | var i :Long = 1; 9 | var num :Long = 1000000000; 10 | while (i <= num) 11 | { 12 | ans += BigInteger.valueOf(i); 13 | i += 1; 14 | } 15 | println(message = ans); 16 | } 17 | -------------------------------------------------------------------------------- /Mathematica.m: -------------------------------------------------------------------------------- 1 | For[i = 1; n = 0, i <= 1000000000, i++, n += i]; 2 | Print[n]; 3 | -------------------------------------------------------------------------------- /Maxima.max: -------------------------------------------------------------------------------- 1 | print(sum(i, i, 1, 10^9))$ 2 | -------------------------------------------------------------------------------- /PHP.php: -------------------------------------------------------------------------------- 1 | 10 | -------------------------------------------------------------------------------- /Pascal.pas: -------------------------------------------------------------------------------- 1 | program sumbill; 2 | 3 | var 4 | I, Sum: Int64; 5 | begin 6 | Sum := 0; 7 | for I := 1 to 1000000000 do begin 8 | Sum := Sum + I; 9 | end; 10 | Writeln(Sum); 11 | end. 12 | -------------------------------------------------------------------------------- /Perl.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | use 5.022; 3 | use warnings; 4 | 5 | my $sum = 0; 6 | $sum += $_ for (1 .. 1000000000); 7 | say $sum; 8 | -------------------------------------------------------------------------------- /Python.py: -------------------------------------------------------------------------------- 1 | #! /bin/python3 2 | print (sum(range(1,1000000001))) 3 | -------------------------------------------------------------------------------- /R.r: -------------------------------------------------------------------------------- 1 | n=sum(1:1000000000); 2 | print(n); 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # one-two-three...infinity 2 | 3 | Calculating the **sum** from one to a billion in different programming languages, inspired by [leachim6/hello-world](https://github.com/leachim6/hello-world) 4 | 5 | ## Current Languages 6 | 7 | - [Agda](Agda.agda) 8 | - [APL](APL.apl) 9 | - [Basic](Basic.bas) 10 | - [Brainfuck](Brainfuck.bf) 11 | - [C](C.c) 12 | - [C++](C++.cpp) 13 | - [C#](C%20Sharp.cs) 14 | - [Cuda](Cuda.cu) 15 | - [D](D.d) 16 | - [Dart](Dart.dart) 17 | - [Erlang](Erlang.erl) 18 | - [Fortran](Fortran.f90) 19 | - [Futhark](Futhark.fut) 20 | - [Go](Go.go) 21 | - [Haskell](Haskell.hs) 22 | - [Java](Java.java) 23 | - [JavaScript](JavaScript.js) 24 | - [Julia](Julia.jl) 25 | - [Kotlin](Koltlin.kt) 26 | - [Common Lisp](Common%20Lisp.lisp) 27 | - [Mathematica](Mathematica.m) 28 | - [Maxima](Maxima.max) 29 | - [Pascal](Pascal.pas) 30 | - [Perl](Perl.pl) 31 | - [PHP](PHP.php) 32 | - [R](R.r) 33 | - [Python](Python.py) 34 | - [Racket](Racket.rkt) 35 | - [Ruby](Ruby.rb) 36 | - [Rust](Rust.rs) 37 | - [Scala](Scala.scala) 38 | - [Shell](Shell.sh) 39 | - [SML](SML.sml) 40 | - [Smalltalk](Smalltalk.st) 41 | - [Swift](Swift.swift) 42 | - [TypeScript](TypeScript.ts) 43 | - [Zig](Zig.zig) 44 | -------------------------------------------------------------------------------- /Racket.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | 3 | (stream-fold + 0 (in-inclusive-range 0 1000000000)) 4 | -------------------------------------------------------------------------------- /Ruby.rb: -------------------------------------------------------------------------------- 1 | p (1..1_000_000_000).sum 2 | -------------------------------------------------------------------------------- /Rust.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let sum: u64 = (1..1_000_000_001).sum(); 3 | println!("sum = {}", sum); 4 | } 5 | -------------------------------------------------------------------------------- /SML.sml: -------------------------------------------------------------------------------- 1 | fun sum acc 0 = acc 2 | | sum acc i = sum (acc + i) (i - 1); 3 | print (Int.toString (sum 0 1000000000)) 4 | -------------------------------------------------------------------------------- /Scala.scala: -------------------------------------------------------------------------------- 1 | object sum { 2 | def main(args: Array[String]) = println((BigInt(1) to BigInt(1000000000L)).sum) 3 | } 4 | -------------------------------------------------------------------------------- /Shell.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | A=0; 3 | for (( i=0; i<=1000000000; i++ )){ 4 | A=$[$A+$i]; 5 | } 6 | echo "$A"; 7 | exit 0; 8 | -------------------------------------------------------------------------------- /Smalltalk.st: -------------------------------------------------------------------------------- 1 | sum := 0. 2 | 1 to: 1000000000 do: [ :i | sum := sum + i ]. 3 | Transcript show: sum. 4 | -------------------------------------------------------------------------------- /Swift.swift: -------------------------------------------------------------------------------- 1 | class ViewController: UIViewController { 2 | func viewDidLoad() { 3 | let n = 1000000000 4 | print (Float(n) * Float(n) / Float(2) + Float(n) / Float(2)) 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /TypeScript.ts: -------------------------------------------------------------------------------- 1 | var j = 0n; 2 | 3 | for (var i = 1n; i <= 1000000000n; i++) { 4 | j += i; 5 | } 6 | 7 | console.log(j.toString()); 8 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /Zig.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | 3 | pub fn main() void { 4 | var sum: i64 = 0; 5 | var i: i64 = 1; 6 | while (i <= 1000000000): (i += 1) { 7 | sum += i; 8 | } 9 | 10 | std.debug.print("{any}\n", .{sum}); 11 | } 12 | 13 | --------------------------------------------------------------------------------