task_url
stringlengths
30
116
task_name
stringlengths
2
86
task_description
stringlengths
0
14.4k
language_url
stringlengths
2
53
language_name
stringlengths
1
52
code
stringlengths
0
61.9k
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Kotlin
Kotlin
import java.math.BigDecimal import java.math.MathContext   val con1024 = MathContext(1024) val bigTwo = BigDecimal(2) val bigFour = bigTwo * bigTwo   fun bigSqrt(bd: BigDecimal, con: MathContext): BigDecimal { var x0 = BigDecimal.ZERO var x1 = BigDecimal.valueOf(Math.sqrt(bd.toDouble())) while (x0 != x1) {...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Arendelle
Arendelle
// Creating an array as [ 23, 12, 2, 5345, 23 ] // with name "space" ( space , 23; 12; 2; 5345; 23 ) // Getting the size of an array: "Size of array is | @space? |" // Appending array with 54 ( space[ @space? ] , 54 ) // Something else fun about arrays in Arendelle // for example when you have one like ...
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#BBC_BASIC
BBC BASIC
DIM Complex{r, i}   DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0 DIM b{} = Complex{} : b.r = PI# : b.i = 1.2 DIM o{} = Complex{}   PROCcomplexadd(o{}, a{}, b{}) PRINT "Result of addition is " FNcomplexshow(o{}) PROCcomplexmul(o{}, a{}, b{}) PRINT "Result of multiplication ...
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#Bracmat
Bracmat
(add=a b.!arg:(?a,?b)&!a+!b) & ( multiply = a b.!arg:(?a,?b)&1+!a*!b+-1 ) & (negate=.1+-1*!arg+-1) & ( conjugate = a b .  !arg:i&-i | !arg:-i&i | !arg:?a_?b&(conjugate$!a)_(conjugate$!b) | !arg ) & ( invert = conjugated . conjugate$!arg:?conjugated & multiply$(!arg,!co...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Fortran
Fortran
SUBROUTINE CHECK(A,N) !Inspect matrix A. REAL A(:,:) !The matrix, whatever size it is. INTEGER N !The order. REAL B(N,N) !A scratchpad, size known on entry.. INTEGER, ALLOCATABLE::TROUBLE(:) !But for this, I'll decide later. INTEGER M   M = COUNT(A(1:N,1:N).LE.0) !Some m...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#FreeBASIC
FreeBASIC
  /' FreeBASIC admite tres tipos básicos de asignación de memoria: - La asignación estática se produce para variables estáticas y globales. La memoria se asigna una vez cuando el programa se ejecuta y persiste durante toda la vida del programa. - La asignación de pila se produce para parámetros de procedimiento y var...
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#C
C
#include <stdio.h> #include <stdlib.h> #define FMT "%lld" typedef long long int fr_int_t; typedef struct { fr_int_t num, den; } frac;   fr_int_t gcd(fr_int_t m, fr_int_t n) { fr_int_t t; while (n) { t = n; n = m % n; m = t; } return m; }   frac frac_new(fr_int_t num, fr_int_t den) { frac a; if (!den) { printf("d...
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#AutoHotkey
AutoHotkey
agm(a, g, tolerance=1.0e-15){ While abs(a-g) > tolerance { an := .5 * (a + g) g := sqrt(a*g) a := an } return a } SetFormat, FloatFast, 0.15 MsgBox % agm(1, 1/sqrt(2))
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#AWK
AWK
#!/usr/bin/awk -f BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#SSEM
SSEM
00101000000000100000000000000000 0. -20 to c 10100000000001100000000000000000 1. c to 5 10100000000000100000000000000000 2. -5 to c 10101000000000010000000000000000 3. Sub. 21 00000000000001110000000000000000 4. Stop 00000000000000000000000000000000 5. 0
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Standard_ML
Standard ML
val () = let val a = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn))) val b = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn))) in print ("a + b = " ^ Int.toString (a + b) ^ "\n"); print ("a - b = " ^ Int.toString (a - b) ^ "\n"); print ("a * b = " ^ Int.toString (a * b) ...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Maple
Maple
agm:=proc(n) local a:=1,g:=evalf(sqrt(1/2)),s:=0,p:=4,i; for i to n do a,g:=(a+g)/2,sqrt(a*g); s+=p*(a*a-g*g); p+=p od; 4*a*a/(1-s) end:   Digits:=100000: d:=agm(16)-evalf(Pi): evalf[10](d); # 4.280696926e-89415
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
pi[n_, prec_] := Module[{a = 1, g = N[1/Sqrt[2], prec], k, s = 0, p = 4}, For[k = 1, k < n, k++, {a, g} = {N[(a + g)/2, prec], N[Sqrt[a g], prec]}; s += p (a^2 - g^2); p += p]; N[4 a^2/(1 - s), prec]]     pi[7, 100] - N[Pi, 100] 1.2026886537*10^-86   pi[7, 100] 3.14159265358979323846264338327950288419716939...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Argile
Argile
use std, array   (:::::::::::::::::  : Static arrays :  :::::::::::::::::) let the array of 2 text aabbArray be Cdata{"aa";"bb"} let raw array of real :my array: = Cdata {1.0 ; 2.0 ; 3.0} (: auto sized :) let another_array be an array of 256 byte (: not initialised :) let (raw array of (array of 3 real)) foobar = Cdata...
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#C
C
#include <complex.h> #include <stdio.h>   void cprint(double complex c) { printf("%f%+fI", creal(c), cimag(c)); } void complex_operations() { double complex a = 1.0 + 1.0I; double complex b = 3.14159 + 1.2I;   double complex c;   printf("\na="); cprint(a); printf("\nb="); cprint(b);   // addition c = a ...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Go
Go
package main   import ( "fmt" "runtime" "sync" )   // New to Go 1.3 are sync.Pools, basically goroutine-safe free lists. // There is overhead in the goroutine-safety and if you do not need this // you might do better by implementing your own free list.   func main() { // Task 1: Define a pool (of ints)...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#J
J
coclass 'integerPool' require 'jmf' create=: monad define Lim=: y*SZI_jmf_ Next=: -SZI_jmf_ Pool=: mema Lim )   destroy=: monad define memf Pool codestroy'' )   alloc=: monad define assert.Lim >: Next=: Next+SZI_jmf_ r=.Pool,Next,1,JINT r set y r )   get=: adverb define memr m )   set=: adverb defin...
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#C.23
C#
using System;   struct Fraction : IEquatable<Fraction>, IComparable<Fraction> { public readonly long Num; public readonly long Denom;   public Fraction(long num, long denom) { if (num == 0) { denom = 1; } else if (denom == 0) { throw new Ar...
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#BASIC
BASIC
PRINT AGM(1, 1 / SQR(2)) END   FUNCTION AGM (a, g) DO ta = (a + g) / 2 g = SQR(a * g) SWAP a, ta LOOP UNTIL a = ta   AGM = a END FUNCTION
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#bc
bc
/* Calculate the arithmethic-geometric mean of two positive * numbers x and y. * Result will have d digits after the decimal point. */ define m(x, y, d) { auto a, g, o   o = scale scale = d d = 1 / 10 ^ d   a = (x + y) / 2 g = sqrt(x * y) while ((a - g) > d) { x = (a + g) / 2 ...
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Swift
Swift
  let a = 6 let b = 4   print("sum =\(a+b)") print("difference = \(a-b)") print("product = \(a*b)") print("Integer quotient = \(a/b)") print("Remainder = (a%b)") print("No operator for Exponential")  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Tcl
Tcl
puts "Please enter two numbers:"   set x [expr {int([gets stdin])}]; # Force integer interpretation set y [expr {int([gets stdin])}]; # Force integer interpretation   puts "$x + $y = [expr {$x + $y}]" puts "$x - $y = [expr {$x - $y}]" puts "$x * $y = [expr {$x * $y}]" puts "$x / $y = [expr {$x / $y}]" puts "$x mod $y =...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#11l
11l
V doors = [0B] * 100 L(i) 100 L(j) (i .< 100).step(i + 1) doors[j] = !doors[j] print(‘Door ’(i + 1)‘: ’(I doors[i] {‘open’} E ‘close’))
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#.D0.9C.D0.9A-61.2F52
МК-61/52
3 П0 1 П1 П4 2 КвКор 1/x П2 1 ^ 4 / П3 ИП3 ИП1 ИП2 + 2 / П5 ИП1 - x^2 ИП4 * - П3 ИП1 ИП2 * КвКор П2 ИП5 П1 КИП4 L0 14 ИП1 x^2 ИП3 / С/П
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Nim
Nim
from math import sqrt import times   import bignum   #---------------------------------------------------------------------------------------------------   func sqrt(value, guess: Int): Int = result = guess while true: let term = value div result if abs(term - result) <= 1: break result = (result ...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#OCaml
OCaml
let limit = 10000 and n = 2800 let x = Array.make (n+1) 2000   let rec g j sum = if j < 1 then sum else let sum = sum * j + limit * x.(j) in x.(j) <- sum mod (j * 2 - 1); g (j - 1) (sum / (j * 2 - 1))   let rec f i carry = if i = 0 then () else let sum = g i 0 in Printf.printf "%04d" (carry + su...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#ARM_Assembly
ARM Assembly
  /* ARM assembly Raspberry PI */ /* program areaString.s */   /* Constantes */ .equ STDOUT, 1 @ Linux output console .equ EXIT, 1 @ Linux syscall .equ WRITE, 4 @ Linux syscall /* Initialized data */ .data szMessStringsch: .ascii "The string is at item : " sZoneconv: .fill 12,1,' ' szCarriageRet...
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#C.23
C#
namespace RosettaCode.Arithmetic.Complex { using System; using System.Numerics;   internal static class Program { private static void Main() { var number = Complex.ImaginaryOne; foreach (var result in new[] { number + number, number * number, -number, 1 / number, ...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Julia
Julia
  matrix = zeros(Float64, (1000,1000,1000)) # use matrix, then when done set variable to 0 to garbage collect the matrix: matrix = 0 # large memory pool will now be collected when needed  
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Kotlin
Kotlin
// Kotlin Native v0.5   import kotlinx.cinterop.*   fun main(args: Array<String>) { memScoped { val intVar1 = alloc<IntVar>() intVar1.value = 1 val intVar2 = alloc<IntVar>() intVar2.value = 2 println("${intVar1.value} + ${intVar2.value} = ${intVar1.value + intVar2.value}") ...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Lua
Lua
pool = {} -- create an "arena" for the variables a,b,c,d pool.a = 1 -- individually allocated.. pool.b = "hello" pool.c = true pool.d = { 1,2,3 } pool = nil -- release reference allowing garbage collection of entire structure
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
f[x_] := x^2
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#C.2B.2B
C++
#include <iostream> #include "math.h" #include "boost/rational.hpp"   typedef boost::rational<int> frac;   bool is_perfect(int c) { frac sum(1, c); for (int f = 2;f < sqrt(static_cast<float>(c)); ++f){   if (c % f == 0) sum += frac(1,f) + frac(1, c/f); } if (sum.denominator() == 1){ return (s...
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#BQN
BQN
AGM ← { (|𝕨-𝕩) ≤ 1e¯15? 𝕨; (0.5×𝕨+𝕩) 𝕊 √𝕨×𝕩 }   1 AGM 1÷√2
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#C
C
#include<math.h> #include<stdio.h> #include<stdlib.h>   double agm( double a, double g ) { /* arithmetic-geometric mean */ double iota = 1.0E-16; double a1, g1;   if( a*g < 0.0 ) { printf( "arithmetic-geometric mean undefined when x*y<0\n" ); exit(1); }   while( fabs(a-g)>iota ) { a1...
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#TI-83_BASIC
TI-83 BASIC
  Prompt A,B Disp "SUM" Pause A+B Disp "DIFFERENCE" Pause A-B Disp "PRODUCT" Pause AB Disp "INTEGER QUOTIENT" Pause int(A/B) Disp "REMAINDER" Pause A-B*int(A/B)  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#TI-89_BASIC
TI-89 BASIC
Local a, b Prompt a, b Disp "Sum: " & string(a + b) Disp "Difference: " & string(a - b) Disp "Product: " & string(a * b) Disp "Integer quotient: " & string(intDiv(a, b)) Disp "Remainder: " & string(remain(a, b))
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#360_Assembly
360 Assembly
* 100 doors 13/08/2015 HUNDOOR CSECT USING HUNDOOR,R12 LR R12,R15 LA R6,0 LA R8,1 step 1 LA R9,100 LOOPI BXH R6,R8,ELOOPI do ipass=1 to 100 (R6) LR R7,R6 SR R7,R6 LR R10...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#PARI.2FGP
PARI/GP
pi(n)=my(a=1,g=2^-.5);(1-2*sum(k=1,n,[a,g]=[(a+g)/2,sqrt(a*g)];(a^2-g^2)<<k))^-1*4*a^2 pi(6)
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Perl
Perl
use Math::BigFloat try => "GMP,Pari";   my $digits = shift || 100; # Get number of digits from command line print agm_pi($digits), "\n";   sub agm_pi { my $digits = shift; my $acc = $digits + 8; my $HALF = Math::BigFloat->new("0.5"); my ($an, $bn, $tn, $pn) = (Math::BigFloat->bone, $HALF->copy->bsqrt($acc), ...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Arturo
Arturo
; empty array arrA: []   ; array with initial values arrB: ["one" "two" "three"]   ; adding an element to an existing array arrB: arrB ++ "four" print arrB   ; another way to add an element append 'arrB "five" print arrB   ; retrieve an element at some index print arrB\1
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#C.2B.2B
C++
#include <iostream> #include <complex> using std::complex;   void complex_operations() { complex<double> a(1.0, 1.0); complex<double> b(3.14159, 1.25);   // addition std::cout << a + b << std::endl; // multiplication std::cout << a * b << std::endl; // inversion std::cout << 1.0 / a << std::endl; // n...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Nim
Nim
  #################################################################################################### # Pool management.   # Pool of objects. type Block[Size: static Positive, T] = ref array[Size, T] Pool[BlockSize: static Positive, T] = ref object blocks: seq[Block[BlockSize, T]] # List of blocks. ...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Oforth
Oforth
Object Class new: MyClass(a, b, c) MyClass new
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#ooRexx
ooRexx
  '============== Class ArenaPool '==============   string buf sys pb,ii   method Setup(sys n) as sys {buf=nuls n : pb=strptr buf : ii=0 : return pb} method Alloc(sys n) as sys {method=pb+ii : ii+=n} method Empty() {buf="" : pb=0 : ii=0}   end class   macro Create(type,name,qty,pool) type name[qty] at ...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#OxygenBasic
OxygenBasic
  '============== Class ArenaPool '==============   string buf sys pb,ii   method Setup(sys n) as sys {buf=nuls n : pb=strptr buf : ii=0 : return pb} method Alloc(sys n) as sys {method=pb+ii : ii+=n} method Empty() {buf="" : pb=0 : ii=0}   end class   macro Create(type,name,qty,pool) type name[qty] at ...
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#Clojure
Clojure
user> 22/7 22/7 user> 34/2 17 user> (+ 37/5 42/9) 181/15
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#C.23
C#
namespace RosettaCode.ArithmeticGeometricMean { using System; using System.Collections.Generic; using System.Globalization;   internal static class Program { private static double ArithmeticGeometricMean(double number, double otherNumber,...
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Toka
Toka
[ ( a b -- ) 2dup ." a+b = " + . cr 2dup ." a-b = " - . cr 2dup ." a*b = " * . cr 2dup ." a/b = " / . ." remainder " mod . cr ] is mathops
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#TUSCRIPT
TUSCRIPT
  $$ MODE TUSCRIPT a=5 b=3 c=a+b c=a-b c=a*b c=a/b c=a%b  
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#4DOS_Batch
4DOS Batch
  @echo off set doors=%@repeat[C,100] do step = 1 to 100 do door = %step to 100 by %step set doors=%@left[%@eval[%door-1],%doors]%@if[%@instr[%@eval[%door-1],1,%doors]==C,O,C]%@right[%@eval[100-%door],%doors] enddo enddo  
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Phix
Phix
with javascript_semantics requires("1.0.0") -- (mpfr_set_default_prec[ision] has been renamed) include mpfr.e mpfr_set_default_precision(-200) -- set precision to 200 decimal places mpfr a = mpfr_init(1), n = mpfr_init(1), g = mpfr_init(1), z = mpfr_init(0.25), half = mpfr_init(0.5), x1 = mpf...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#AutoHotkey
AutoHotkey
myArray := Object() ; could use JSON-syntax sugar like {key: value} myArray[1] := "foo" myArray[2] := "bar" MsgBox % myArray[2]   ; Push a value onto the array myArray.Insert("baz")
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#Clojure
Clojure
(ns rosettacode.arithmetic.cmplx (:require [clojure.algo.generic.arithmetic :as ga]) (:import [java.lang Number]))   (defrecord Complex [^Number r ^Number i] Object (toString [{:keys [r i]}] (apply str (cond (zero? r) [(if (= i 1) "" i) "i"] (zero? i) [r]  :else [r (if (neg?...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#PARI.2FGP
PARI/GP
pari_init(1<<20, 0); // Initialize PARI with a stack size of 1 MB. GEN four = addii(gen_2, gen_2); // On the stack GEN persist = gclone(four); // On the heap
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Pascal
Pascal
procedure New (var P: Pointer);
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Phix
Phix
without js atom mem = allocate(size,true)
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#PicoLisp
PicoLisp
  (malloc 1000 'raw)  ; raw allocation, bypass the GC, requires free()-ing (malloc 1000 'uncollectable)  ; no GC, for use with other GCs that Racket can be configured with (malloc 1000 'atomic)  ; a block of memory without internal pointers (malloc 1000 'nonatomic)  ; a block of pointers (mallo...
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#Common_Lisp
Common Lisp
(loop for candidate from 2 below (expt 2 19) for sum = (+ (/ candidate) (loop for factor from 2 to (isqrt candidate) when (zerop (mod candidate factor)) sum (+ (/ factor) (/ (floor candidate factor))))) when (= sum 1) collect can...
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#C.2B.2B
C++
  #include<bits/stdc++.h> using namespace std; #define _cin ios_base::sync_with_stdio(0); cin.tie(0); #define rep(a, b) for(ll i =a;i<=b;++i)   double agm(double a, double g) //ARITHMETIC GEOMETRIC MEAN { double epsilon = 1.0E-16,a1,g1; if(a*g<0.0) { cout<<"Couldn't find arithmetic-geometric mean of these numbers\n"...
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#Clojure
Clojure
(ns agmcompute (:gen-class))   ; Java Arbitray Precision Library (import '(org.apfloat Apfloat ApfloatMath))   (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.0...
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#UNIX_Shell
UNIX Shell
#!/bin/sh read a; read b; echo "a+b = " `expr $a + $b` echo "a-b = " `expr $a - $b` echo "a*b = " `expr $a \* $b` echo "a/b = " `expr $a / $b` # truncates towards 0 echo "a mod b = " `expr $a % $b` # same sign as first operand
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Ursa
Ursa
# # integer arithmetic #   decl int x y out "number 1: " console set x (in int console) out "number 2: " console set y (in int console)   out "\nsum:\t" (int (+ x y)) endl console out "diff:\t" (int (- x y)) endl console out "prod:\t" (int (* x y)) endl console # quotient doesn't round at all, but the int function roun...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#6502_Assembly
6502 Assembly
; 100 DOORS in 6502 assembly language for: http://www.6502asm.com/beta/index.html ; Written for the original MOS Technology, Inc. NMOS version of the 6502, but should work with any version. ; Based on BASIC QB64 unoptimized version: http://rosettacode.org/wiki/100_doors#BASIC ; ; Notes: ; Doors array[1..100] is at ...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#PicoLisp
PicoLisp
(scl 40)   (de pi () (let (A 1.0 N 1.0 Z 0.25 G (/ (* 1.0 1.0) (sqrt 2.0 1.0)) ) (use (X1 X2 V) (do 18 (setq X1 (/ (* (+ A G) 0.5) 1.0) X2 (sqrt (* A G)) V (- X1 A) Z (- Z (/ (* (/ (* V V) 1.0) N) 1.0)) ...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Python
Python
from decimal import *   D = Decimal getcontext().prec = 100 a = n = D(1) g, z, half = 1 / D(2).sqrt(), D(0.25), D(0.5) for i in range(18): x = [(a + g) * half, (a * g).sqrt()] var = x[0] - a z -= var * var * n n += n a, g = x print(a * a / z)
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#AutoIt
AutoIt
  #include <Array.au3> ;Include extended Array functions (_ArrayDisplay)   Local $aInputs[1] ;Create the Array with just 1 element   While True ;Endless loop $aInputs[UBound($aInputs) - 1] = InputBox("Array", "Add one value") ;Save user input to the last element of the Array If $aInputs[UBound($aInputs) - 1] = "" The...
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#COBOL
COBOL
$SET SOURCEFORMAT "FREE" $SET ILUSING "System" $SET ILUSING "System.Numerics" class-id Prog. method-id. Main static. procedure division. declare num as type Complex = type Complex::ImaginaryOne() declare results as type Complex occurs any set content of results to ((num + num), (num * num), (- num), (...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#PL.2FI
PL/I
  (malloc 1000 'raw)  ; raw allocation, bypass the GC, requires free()-ing (malloc 1000 'uncollectable)  ; no GC, for use with other GCs that Racket can be configured with (malloc 1000 'atomic)  ; a block of memory without internal pointers (malloc 1000 'nonatomic)  ; a block of pointers (mallo...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Python
Python
  (malloc 1000 'raw)  ; raw allocation, bypass the GC, requires free()-ing (malloc 1000 'uncollectable)  ; no GC, for use with other GCs that Racket can be configured with (malloc 1000 'atomic)  ; a block of memory without internal pointers (malloc 1000 'nonatomic)  ; a block of pointers (mallo...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Racket
Racket
  (malloc 1000 'raw)  ; raw allocation, bypass the GC, requires free()-ing (malloc 1000 'uncollectable)  ; no GC, for use with other GCs that Racket can be configured with (malloc 1000 'atomic)  ; a block of memory without internal pointers (malloc 1000 'nonatomic)  ; a block of pointers (mallo...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Raku
Raku
/*REXX doesn't have declarations/allocations of variables, */ /* but this is the closest to an allocation: */   stemmed_array.= 0 /*any undefined element will have this value. */   stemmed_array.1 = '1st entry' stemmed_array.2 = '2nd entry' stemmed_array.6000 = 12 ** 2 stemmed_array.dog = stemme...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#REXX
REXX
/*REXX doesn't have declarations/allocations of variables, */ /* but this is the closest to an allocation: */   stemmed_array.= 0 /*any undefined element will have this value. */   stemmed_array.1 = '1st entry' stemmed_array.2 = '2nd entry' stemmed_array.6000 = 12 ** 2 stemmed_array.dog = stemme...
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#D
D
import std.bigint, std.traits, std.conv;   // std.numeric.gcd doesn't work with BigInt. T gcd(T)(in T a, in T b) pure nothrow { return (b != 0) ? gcd(b, a % b) : (a < 0) ? -a : a; }   T lcm(T)(in T a, in T b) pure nothrow { return a / gcd(a, b) * b; }   struct RationalT(T) if (!isUnsigned!T) { private T num...
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#COBOL
COBOL
IDENTIFICATION DIVISION. PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG. DATA DIVISION. WORKING-STORAGE SECTION. 01 AGM-VARS. 05 A PIC 9V9(16). 05 A-ZERO PIC 9V9(16). 05 G PIC 9V9(16). 05 DIFF PIC 9V9(16) VALUE 1. * Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH * is never p...
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#VBA
VBA
  'Arithmetic - Integer Sub RosettaArithmeticInt() Dim opr As Variant, a As Integer, b As Integer On Error Resume Next   a = CInt(InputBox("Enter first integer", "XLSM | Arithmetic")) b = CInt(InputBox("Enter second integer", "XLSM | Arithmetic"))   Debug.Print "a ="; a, "b="; b, vbCr For Each opr In Split("+ - * / \ m...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#68000_Assembly
68000 Assembly
*----------------------------------------------------------- * Title  : 100Doors.X68 * Written by : G. A. Tippery * Date  : 2014-01-17 * Description: Solves "100 Doors" problem, see: http://rosettacode.org/wiki/100_doors * Notes  : Translated from C "Unoptimized" version, http://rosettacode.org/wiki/100_do...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#R
R
library(Rmpfr)   agm <- function(n, prec) { s <- mpfr(0, prec) a <- mpfr(1, prec) g <- sqrt(mpfr("0.5", prec)) p <- as.bigz(4) for (i in seq(n)) { m <- (a + g) / 2L g <- sqrt(a * g) a <- m s <- s + p * (a * a - g * g) p <- p + p } 4L * a * a / (1L - s) }   1e6 * log(10) / log(2) # [1] ...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Racket
Racket
#lang racket (require math/bigfloat)   (define (pi/a-g rep) (let loop ([a 1.bf] [g (bf1/sqrt 2.bf)] [z (bf/ 1.bf 4.bf)] [n (bf 1)] [r 0]) (if (< r rep) (let* ([a-p (bf/ (bf+ a g) 2.bf)] [g-p (bfsqrt (bf* a g))] [z-p (bf- z (...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Avail
Avail
tup ::= <"aardvark", "cat", "dog">;
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#CoffeeScript
CoffeeScript
  # create an immutable Complex type class Complex constructor: (@r=0, @i=0) -> @magnitude = @r*@r + @i*@i   plus: (c2) -> new Complex( @r + c2.r, @i + c2.i )   times: (c2) -> new Complex( @r*c2.r - @i*c2.i, @r*c2.i + @i*c2.r )   negation: -> new Complex( -1...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Rust
Rust
#![feature(rustc_private)]   extern crate arena;   use arena::TypedArena;   fn main() { // Memory is allocated using the default allocator (currently jemalloc). The memory is // allocated in chunks, and when one chunk is full another is allocated. This ensures that // references to an arena don't become i...
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Scala
Scala
package require Tcl 8.6 oo::class create Pool { superclass oo::class variable capacity pool busy unexport create constructor args { next {*}$args set capacity 100 set pool [set busy {}] } method new {args} { if {[llength $pool]} { set pool [lassign $pool obj] } else { if {[llength...
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#Delphi
Delphi
  program Arithmetic_Rational;   {$APPTYPE CONSOLE}   uses System.SysUtils, Boost.Rational;   var sum: TFraction; max: Integer = 1 shl 19; candidate, max2, factor: Integer;   begin for candidate := 2 to max - 1 do begin sum := Fraction(1, candidate); max2 := Trunc(Sqrt(candidate)); for factor ...
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#Common_Lisp
Common Lisp
(defun agm (a0 g0 &optional (tolerance 1d-8)) (loop for a = a0 then (* (+ a g) 5d-1) and g = g0 then (sqrt (* a g)) until (< (abs (- a g)) tolerance) finally (return a)))  
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#D
D
import std.stdio, std.math, std.meta, std.typecons;   real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { //{a, g} = {(a + g) / 2.0, sqrt(a * g)}; AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; }   void main(...
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#VBScript
VBScript
option explicit dim a, b wscript.stdout.write "A? " a = wscript.stdin.readline wscript.stdout.write "B? " b = wscript.stdin.readline   a = int( a ) b = int( b )   wscript.echo "a + b=", a + b wscript.echo "a - b=", a - b wscript.echo "a * b=", a * b wscript.echo "a / b=", a / b wscript.echo "a \ b=", a \ b wscript.echo...
http://rosettacode.org/wiki/Arithmetic_evaluation
Arithmetic evaluation
Create a program which parses and evaluates arithmetic expressions. Requirements An abstract-syntax tree (AST) for the expression must be created from parsing the input. The AST must be used in evaluation, also, so the input may not be directly evaluated (e.g. by calling eval or a similar language feature.) The ...
#11l
11l
T Symbol String id Int lbp Int nud_bp Int led_bp (ASTNode -> ASTNode) nud ((ASTNode, ASTNode) -> ASTNode) led   F set_nud_bp(nud_bp, nud) .nud_bp = nud_bp .nud = nud   F set_led_bp(led_bp, led) .led_bp = led_bp .led = led   T ASTNode Symbol& symbol Int value ASTN...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#8080_Assembly
8080 Assembly
page: equ 2 ; Store doors in page 2 doors: equ 100 ; 100 doors puts: equ 9 ; CP/M string output org 100h xra a ; Set all doors to zero lxi h,256*page mvi c,doors zero: mov m,a inx h dcr c jnz zero mvi m,'$' ; CP/M string terminator (for easier output later) mov d,a ; D=0 so that DE=E=pass counter mov e,a ; E=...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#Raku
Raku
constant number-of-decimals = 100;   multi sqrt(Int $n) { my $guess = 10**($n.chars div 2); my $iterator = { ( $^x + $n div ($^x) ) div 2 }; my $endpoint = { $^x == $^y|$^z }; return min (+$guess, $iterator … $endpoint)[*-1, *-2]; }   multi sqrt(FatRat $r --> FatRat) { return FatRat.new: sqr...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#AWK
AWK
BEGIN { # to make an array, assign elements to it array[1] = "first" array[2] = "second" array[3] = "third" alen = 3 # want the length? store in separate variable   # or split a string plen = split("2 3 5 7 11 13 17 19 23 29", primes) clen = split("Ottawa;Washington DC;Mexico City", cities, ";")   # ...
http://rosettacode.org/wiki/Arithmetic/Complex
Arithmetic/Complex
A   complex number   is a number which can be written as: a + b × i {\displaystyle a+b\times i} (sometimes shown as: b + a × i {\displaystyle b+a\times i} where   a {\displaystyle a}   and   b {\displaystyle b}   are real numbers,   and   i {\displaystyle i}   is   √ -1  Typica...
#Common_Lisp
Common Lisp
> (sqrt -1) #C(0.0 1.0)   > (expt #c(0 1) 2) -1
http://rosettacode.org/wiki/Arena_storage_pool
Arena storage pool
Dynamically allocated objects take their memory from a heap. The memory for an object is provided by an allocator which maintains the storage pool used for the heap. Often a call to allocator is denoted as P := new T where   T   is the type of an allocated object,   and   P   is a reference to the object. The stora...
#Tcl
Tcl
package require Tcl 8.6 oo::class create Pool { superclass oo::class variable capacity pool busy unexport create constructor args { next {*}$args set capacity 100 set pool [set busy {}] } method new {args} { if {[llength $pool]} { set pool [lassign $pool obj] } else { if {[llength...
http://rosettacode.org/wiki/Arithmetic/Rational
Arithmetic/Rational
Task Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language. Example Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Fur...
#EchoLisp
EchoLisp
  ;; Finding perfect numbers (define (sum/inv n) ;; look for div's in [2..sqrt(n)] and add 1/n (for/fold (acc (/ n)) [(i (in-range 2 (sqrt n)))] #:break (> acc 1) ; no hope (when (zero? (modulo n i )) (set! acc (+ acc (/ i) (/ i n))))))  
http://rosettacode.org/wiki/Arithmetic-geometric_mean
Arithmetic-geometric mean
This page uses content from Wikipedia. The original article was at Arithmetic-geometric mean. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Task Write a function to compute the arithmetic-geomet...
#Delphi
Delphi
  program geometric_mean;   {$APPTYPE CONSOLE}   uses System.SysUtils;   function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end;   function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('...
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Vedit_macro_language
Vedit macro language
#1 = Get_Num("Give number a: ") #2 = Get_Num("Give number b: ") Message("a + b = ") Num_Type(#1 + #2) Message("a - b = ") Num_Type(#1 - #2) Message("a * b = ") Num_Type(#1 * #2) Message("a / b = ") Num_Type(#1 / #2) Message("a % b = ") Num_Type(#1 % #2)
http://rosettacode.org/wiki/Arithmetic_evaluation
Arithmetic evaluation
Create a program which parses and evaluates arithmetic expressions. Requirements An abstract-syntax tree (AST) for the expression must be created from parsing the input. The AST must be used in evaluation, also, so the input may not be directly evaluated (e.g. by calling eval or a similar language feature.) The ...
#Ada
Ada
INT base=10; MODE FIXED = LONG REAL; # numbers in the format 9,999.999 #   #IF build abstract syntax tree and then EVAL tree # MODE AST = UNION(NODE, FIXED); MODE NUM = REF AST; MODE NODE = STRUCT(NUM a, PROC (FIXED,FIXED)FIXED op, NUM b);   OP EVAL = (NUM ast)FIXED:( CASE ast IN (FIXED num): num, (NODE fork)...
http://rosettacode.org/wiki/100_doors
100 doors
There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it). The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it. The third...
#8086_Assembly
8086 Assembly
  \ Array of doors; init to empty; accessing a non-extant member will return \ 'null', which is treated as 'false', so we don't need to initialize it: [] var, doors   \ given a door number, get the value and toggle it: : toggle-door \ n -- doors @ over a:@ not rot swap a:! drop ;   \ print which doors are open: :...
http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
Arithmetic-geometric mean/Calculate Pi
Almkvist Berndt 1988 begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate π {\displaystyle \pi } . With the same notations used in Arithmetic-geometric mean, we can summarize the paper by writing: π ...
#REXX
REXX
/*REXX program calculates the value of pi using the AGM algorithm. */ parse arg d .; if d=='' | d=="," then d= 500 /*D not specified? Then use default. */ numeric digits d+5 /*set the numeric decimal digits to D+5*/ z= 1/4; a= 1; g= sqrt(1/2) ...
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available,...
#Axe
Axe
1→{L₁} 2→{L₁+1} 3→{L₁+2} 4→{L₁+3} Disp {L₁}►Dec,i Disp {L₁+1}►Dec,i Disp {L₁+2}►Dec,i Disp {L₁+3}►Dec,i