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/Factors_of_an_integer
Factors of an 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 |...
#PL.2FI
PL/I
factors: procedure options(main); declare i binary( 15 )fixed; declare n binary( 15 )fixed; do n = 90 to 100; put skip list( 'factors of: ', n, ': ' ); do i = 1 to n; if mod(n, i) = 0 then put edit( i )(f(4)); end; end; end factors;  
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#11l
11l
F hello() print(‘Hello, world!’)   String src F quine() print(:src)   F bottles() L(i) (99.<2).step(-1) print(‘#. bottles of beer on the wall’.format(i)) print(‘#. bottles of beer’.format(i)) print(‘Take one down, pass it around’) print(‘#. bottles of beer on the wall’.format(i - 1)) ...
http://rosettacode.org/wiki/Execute_Computer/Zero
Execute Computer/Zero
Execute Computer/Zero is an implementation of Computer/zero Assembly. Other implementations of Computer/zero Assembly. Task Create a Computer/zero Assembly emulator. You may consider this webpage as a reference implementation. Output the results of the sample programs "2+2" and "7*8" found there.   The virtual machi...
#Wren
Wren
var NOP = 0 var LDA = 1 var STA = 2 var ADD = 3 var SUB = 4 var BRZ = 5 var JMP = 6 var STP = 7   var ops = {"NOP": NOP, "LDA": LDA, "STA": STA, "ADD": ADD, "SUB": SUB, "BRZ": BRZ, "JMP": JMP, "STP": STP}   var load = Fn.new { |prog| var mem = List.filled(32, 0) var lines = prog.trimEnd().split("\n")...
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#MUMPS
MUMPS
USER>write 3e145 30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 USER>write 3e146 <MAXNUMBER>
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#NetRexx
NetRexx
/* NetRexx */ options replace format comments java crossref symbols binary   negInf = double -1.0 / 0.0; knegInf = Double.NEGATIVE_INFINITY inf = double 1.0 / 0.0; kinf = Double.POSITIVE_INFINITY nan = double 0.0 / 0.0; knan = Double.NaN negZero = double -2.0 / inf; knegZero = -2.0 / Double.POSITIVE...
http://rosettacode.org/wiki/Execute_SNUSP
Execute SNUSP
Execute SNUSP is an implementation of SNUSP. Other implementations of SNUSP. RCSNUSP SNUSP An implementation need only properly implement the Core SNUSP instructions ('$', '\', '/', '+', '-', '<', '>', ',', '.', '!', and '?'). Modular SNUSP ('#', '@') and Bloated SNUSP (':', ';', '%', and '&') are also allowed, but not...
#C
C
# # snusp.icn, A Modular SNUSP interpreter #   $define VERSION 0.6   # allow a couple of cli options link options   # directions $define DRIGHT 1 $define DLEFT 2 $define DUP 3 $define DDOWN 4   record position(row, col) global dir, ip, ram   procedure main(argv) local ch, codespace, col, dp, fn, line local r...
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#Coq
Coq
  Notation "A /\ B" := (and A B)  
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#D
D
void if2(T1, T2, T3, T4)(in bool c1, in bool c2, lazy T1 first, lazy T2 both, lazy T3 second, lazy T4 none) { if (c1) { if (c2) both; else first; } else { if (c2) ...
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#CLU
CLU
start_up = proc () po: stream := stream$primary_output()   stream$putl(po, "5**3**2 = " || int$unparse(5**3**2)) stream$putl(po, "(5**3)**2 = " || int$unparse((5**3)**2)) stream$putl(po, "5**(3**2) = " || int$unparse(5**(3**2))) end start_up
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Common_Lisp
Common Lisp
(expt (expt 5 3) 2) (expt 5 (expt 3 2))
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#D
D
void main() { import std.stdio, std.math, std.algorithm;   writefln("5 ^^ 3 ^^ 2 = %7d", 5 ^^ 3 ^^ 2); writefln("(5 ^^ 3) ^^ 2 = %7d", (5 ^^ 3) ^^ 2); writefln("5 ^^ (3 ^^ 2) = %7d", 5 ^^ (3 ^^ 2)); writefln("[5, 3, 2].reduce!pow = %7d", [5, 3, 2].reduce!pow); }
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#REXX
REXX
/*REXX program calculates and displays factorions in bases nine ───► twelve. */ parse arg LOb HIb lim . /*obtain optional arguments from the CL*/ if LOb=='' | LOb=="," then LOb= 9 /*Not specified? Then use the default.*/ if HIb=='' | HIb=="," then HIb= 12 ...
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#Ruby
Ruby
  def factorion?(n, base) n.digits(base).sum{|digit| (1..digit).inject(1, :*)} == n end   (9..12).each do |base| puts "Base #{base} factorions: #{(1..1_500_000).select{|n| factorion?(n, base)}.join(" ")} " end  
http://rosettacode.org/wiki/Filter
Filter
Task Select certain elements from an Array into a new Array in a generic way. To demonstrate, select all even numbers from an Array. As an option, give a second solution which filters destructively, by modifying the original Array rather than creating a new Array.
#VBA
VBA
  Option Explicit   Sub Main() Dim evens() As Long, i As Long Dim numbers() As Long   For i = 1 To 100000 ReDim Preserve numbers(1 To i) numbers(i) = i Next i   evens = FilterInNewArray(numbers)   Debug.Print "Count of initial array : " & UBound(numbers) & ", first item : " & numbers(LBo...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#PowerShell
PowerShell
for ($i = 1; $i -le 100; $i++) { if ($i % 15 -eq 0) { "FizzBuzz" } elseif ($i % 5 -eq 0) { "Buzz" } elseif ($i % 3 -eq 0) { "Fizz" } else { $i } }
http://rosettacode.org/wiki/Extensible_prime_generator
Extensible prime generator
Task Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime. The routine should demonstrably rely on either: Being based on an open-ended counter set to count without upper limit other than system or programming language limits. In thi...
#Dart
Dart
Iterable<int> primesMap() { Iterable<int> oddprms() sync* { yield(3); yield(5); // need at least 2 for initialization final Map<int, int> bpmap = {9: 6}; final Iterator<int> bps = oddprms().iterator; bps.moveNext(); bps.moveNext(); // skip past 3 to 5 int bp = bps.current; int n ...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#F.C5.8Drmul.C3.A6
Fōrmulæ
fib := function(n) local a; a := [[0, 1], [1, 1]]^n; return a[1][2]; end;
http://rosettacode.org/wiki/Factors_of_an_integer
Factors of an 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 |...
#PL.2FM
PL/M
To run: Start up. Show the factors of 11. Show the factors of 21. Show the factors of 519. Wait for the escape key. Shut down.   To show the factors of a number: Write "The factors of " then the number then ":" on the console. Find a square root of the number. Loop. If a counter is past the square root, write "" on the...
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#8080_Assembly
8080 Assembly
putch: equ 2 ; Write character puts: equ 9 ; Write string fopen: equ 15 ; Open file fread: equ 20 ; Read record setdma: equ 26 ; Set DMA address fcb: equ 5Ch ; FCB for first file on command line org 100h ;;; Open source file given on command line lxi d,fcb mvi c,fopen call 5 ; Open file inr a ; A=FF = error lxi ...
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#Action.21
Action!
PROC Run(CHAR ARRAY code) BYTE i,a CHAR c   PrintF("Run ""%S""%E%E",code) a=0 FOR i=1 TO code(0) DO c=code(i) IF c='q OR c='Q THEN PrintE(code) ELSEIF c='h OR c='H THEN PrintE("Hello, world!") ELSEIF c='9 THEN PrintE("99 bottles here...") ELSEIF c='+ THEN a==+1 ...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#11l
11l
F my_pow(base, exp) -> Float I exp < 0 R 1 / my_pow(base, -exp) I exp == 0 R 1 V ans = base L 0 .< exp - 1 ans *= base R ans   print(‘2 ^ 3 = ’my_pow(2, 3)) print(‘1 ^ -10 = ’my_pow(1, -10)) print(‘-1 ^ -3 = ’my_pow(-1, -3)) print() print(‘2.0 ^ -3 = ’my_pow(2.0, -3)) print(‘1.5 ^ ...
http://rosettacode.org/wiki/Execute_Computer/Zero
Execute Computer/Zero
Execute Computer/Zero is an implementation of Computer/zero Assembly. Other implementations of Computer/zero Assembly. Task Create a Computer/zero Assembly emulator. You may consider this webpage as a reference implementation. Output the results of the sample programs "2+2" and "7*8" found there.   The virtual machi...
#Z80_Assembly
Z80 Assembly
org &1000 PrintChar equ &BB5A   ;reg usage: ;DE = VM'S Program Counter ;IXL = VM's Accumulator main: ld ixl,0 ld de,Computer_Zero_RAM_2_plus_2 call Computer_Zero_VM ld a,ixl call ShowHex ;output to Amstrad CPC's screen call NewLine   ld ixl,0 ld de,Computer_Zero_RAM_7x8 call Computer_Zero_VM ld a,ixl jp Show...
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#Nim
Nim
echo 1e234 * 1e234 # inf echo 1e234 * -1e234 # -inf echo 1 / Inf # 0 echo Inf + -Inf # nan echo NaN # nan   echo NaN == NaN # false echo 0.0 == -0.0 # true echo 0.0 * NaN # nan echo NaN * 0.0 # nan echo 0.0 * Inf # nan echo Inf * 0.0 # nan
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#OCaml
OCaml
# infinity;; - : float = infinity # neg_infinity;; - : float = neg_infinity # nan;; - : float = nan # -0.;; - : float = -0. # -. 0.;; - : float = -0. # 1. /. 0.;; - : float = infinity # -1. /. 0.;; - : float = neg_infinity # -. infinity;; - : float = neg_infinity # infinity +. neg_infinity;; - : float = nan # 0. /. 0.;...
http://rosettacode.org/wiki/Execute_SNUSP
Execute SNUSP
Execute SNUSP is an implementation of SNUSP. Other implementations of SNUSP. RCSNUSP SNUSP An implementation need only properly implement the Core SNUSP instructions ('$', '\', '/', '+', '-', '<', '>', ',', '.', '!', and '?'). Modular SNUSP ('#', '@') and Bloated SNUSP (':', ';', '%', and '&') are also allowed, but not...
#C.2B.2B
C++
# # snusp.icn, A Modular SNUSP interpreter #   $define VERSION 0.6   # allow a couple of cli options link options   # directions $define DRIGHT 1 $define DLEFT 2 $define DUP 3 $define DDOWN 4   record position(row, col) global dir, ip, ram   procedure main(argv) local ch, codespace, col, dp, fn, line local r...
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#Delphi
Delphi
  procedure Check(Condition1: Boolean; Condition2: Boolean) begin if Condition1 = True then begin if Condition2 = True then BothConditionsAreTrue else FirstConditionIsTrue; end else if Condition2 = True then SecondConditionIsTrue else NoConditionIsTrue; end;
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#EchoLisp
EchoLisp
  ;; the standard and secure way is to use the (expt a b) function (expt 5 (expt 3 2)) ;; 5 ** ( 3 ** 2) → 1953125 (expt (expt 5 3) 2) ;; (5 ** 3) ** 2 → 15625   ;; infix EchoLisp may use the ** operator, which right associates (lib 'match) (load 'infix.glisp)   (5 ** 3 ** 2) → 1953125 ((5 ** 3) ** 2) ...
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Factor
Factor
USING: formatting math.functions ;   5 3 2 ^ ^ "5 3 2 ^ ^  %d\n" printf   5 3 ^ 2 ^ "5 3 ^ 2 ^  %d\n" printf
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Fortran
Fortran
write(*, "(a, i0)") "5**3**2 = ", 5**3**2 write(*, "(a, i0)") "(5**3)**2 = ", (5**3)**2 write(*, "(a, i0)") "5**(3**2) = ", 5**(3**2)
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#Scala
Scala
object Factorion extends App { private def is_factorion(i: Int, b: Int): Boolean = { var sum = 0L var j = i while (j > 0) { sum += f(j % b) j /= b } sum == i }   private val f = Array.ofDim[Long](12) f(0) = 1L (1 until 12).foreach(n =>...
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#Sidef
Sidef
func max_power(b = 10) { var m = 1 var f = (b-1)! while (m*f >= b**(m-1)) { m += 1 } return m-1 }   func factorions(b = 10) {   var result = [] var digits = @^b var fact = digits.map { _! }   for k in (1 .. max_power(b)) { digits.combinations_with_repetition(k, {|*com...
http://rosettacode.org/wiki/Filter
Filter
Task Select certain elements from an Array into a new Array in a generic way. To demonstrate, select all even numbers from an Array. As an option, give a second solution which filters destructively, by modifying the original Array rather than creating a new Array.
#VBScript
VBScript
  test_arr_1 = Array(1,2,3,4,5,6,7,8,9,10) test_arr_2 = Array(1,2,3,4,5,6,7,8,9,10)   WScript.StdOut.Write "Scenario 1: Create a new array" WScript.StdOut.WriteLine WScript.StdOut.Write "Input: " & Join(test_arr_1,",") WScript.StdOut.WriteLine WScript.StdOut.Write "Output: " & filter_create(test_arr_1) WScript.StdOut.W...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Processing
Processing
for (int i = 0; i < width; i++) { if (i % 3 == 0 && i % 5 == 0) { stroke(255, 255, 0); println("FizzBuzz!"); } else if (i % 5 == 0) { stroke(0, 255, 0); println("Buzz"); } else if (i % 3 == 0) { stroke(255, 0, 0); println("Fizz"); } else { stroke(0, 0, 255); println(i); }...
http://rosettacode.org/wiki/Extensible_prime_generator
Extensible prime generator
Task Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime. The routine should demonstrably rely on either: Being based on an open-ended counter set to count without upper limit other than system or programming language limits. In thi...
#Delphi
Delphi
  ; the first twenty primes (primes 20) → { 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 }   ; a stream to generate primes from a (define (primes-from a) (let ((p (next-prime a))) (stream-cons p (primes-from p))))   ; primes between 100,150 (for/list ((p (primes-from 100))) #:break (> p 150) ...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#GAP
GAP
fib := function(n) local a; a := [[0, 1], [1, 1]]^n; return a[1][2]; end;
http://rosettacode.org/wiki/Factors_of_an_integer
Factors of an 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 |...
#Plain_English
Plain English
To run: Start up. Show the factors of 11. Show the factors of 21. Show the factors of 519. Wait for the escape key. Shut down.   To show the factors of a number: Write "The factors of " then the number then ":" on the console. Find a square root of the number. Loop. If a counter is past the square root, write "" on the...
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#Ada
Ada
# HQ9+ interpreter   # execute an HQ9+ program in the code string - code is not case sensitive hq9 := proc( code :: string ) is local hq9Accumulator := 0; # the HQ9+ accumulator local hq9Operations  := # table of HQ9+ operations and their implemntations [ "q" ~ proc() is print( code ) end ...
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#Agena
Agena
# HQ9+ interpreter   # execute an HQ9+ program in the code string - code is not case sensitive hq9 := proc( code :: string ) is local hq9Accumulator := 0; # the HQ9+ accumulator local hq9Operations  := # table of HQ9+ operations and their implemntations [ "q" ~ proc() is print( code ) end ...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#68000_Assembly
68000 Assembly
ExponentUnsigned: ;input: D0.W = BASE ; D1.W = EXPONENT ; OUTPUTS TO D0 ; NO OVERFLOW PROTECTION - USE AT YOUR OWN RISK! ;HIGH WORDS OF D0 AND D1 ARE CLEARED. ;clobbers D1   MOVE.L D2,-(SP)   ;using DBRAs lets us simultaneously subtract and compare DBRA D1,.test_if_one MOVEQ.L #1,D0 ;executes only if D1 w...
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#Oforth
Oforth
>10.0 1000.0 powf PInf == println 1 ok >10.0 1000.0 powf neg NInf == println 1 ok
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#Ol
Ol
(import (scheme inexact))   (print "infinity: " (/ 1 0)) (print "minus infinity: " (log 0))   ; note: (sqrt -1) function will produce 0+i complex number ; so we need to use simpler function "fsqrt"   (import (owl math fp)) (print "not-a-number: " (fsqrt -1))   ; note: your must use equal? or eqv? but not eq? for compar...
http://rosettacode.org/wiki/Execute_SNUSP
Execute SNUSP
Execute SNUSP is an implementation of SNUSP. Other implementations of SNUSP. RCSNUSP SNUSP An implementation need only properly implement the Core SNUSP instructions ('$', '\', '/', '+', '-', '<', '>', ',', '.', '!', and '?'). Modular SNUSP ('#', '@') and Bloated SNUSP (':', ';', '%', and '&') are also allowed, but not...
#COBOL
COBOL
# # snusp.icn, A Modular SNUSP interpreter #   $define VERSION 0.6   # allow a couple of cli options link options   # directions $define DRIGHT 1 $define DLEFT 2 $define DUP 3 $define DDOWN 4   record position(row, col) global dir, ip, ram   procedure main(argv) local ch, codespace, col, dp, fn, line local r...
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#DUP
DUP
{two-conditional if operator implementation} { [ top cond. = true ][ top cond. = false ]} { [ 2nd = true ][2nd = false ] [ 2nd = true ][ 2nd = false] } [(((([[)))!)))%%%%%][)))))!)%%%%%]?][[))))!))%%%%%][))))))!%%%%%]?]?]⇒¿  
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#E
E
pragma.enable("lambda-args") # The feature is still experimental syntax   def makeIf2Control(evalFn, tf, ft, ff) { return def if2Control { to only1__control_0(tf) { return makeIf2Control(evalFn, tf, ft, ff) } to only2__control_0(ft) { return makeIf2Control(evalFn, tf, ft, ff) } to else__control_0 (ff) { r...
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#FreeBASIC
FreeBASIC
' FB 1.05.0   ' The exponentation operator in FB is ^ rather than **. ' In the absence of parenthesis this operator is ' left-associative. So the first example ' will have the same value as the second example.   Print "5^3^2 =>"; 5^3^2 Print "(5^3)^2 =>"; (5^3)^2 Print "5^(3^2) =>"; 5^(3^2) Sleep
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Frink
Frink
println["5^3^2 = " + 5^3^2] println["(5^3)^2 = " + (5^3)^2] println["5^(3^2) = " + 5^(3^2)]  
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Go
Go
package main   import "fmt" import "math"   func main() { var a, b, c float64 a = math.Pow(5, math.Pow(3, 2)) b = math.Pow(math.Pow(5, 3), 2) c = math.Pow(5, math.Pow(3, 2)) fmt.Printf("5^3^2 = %.0f\n", a) fmt.Printf("(5^3)^2 = %.0f\n", b) fmt.Printf("5^(3^2) = %.0f\n", c) }
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#Swift
Swift
var fact = Array(repeating: 0, count: 12)   fact[0] = 1   for n in 1..<12 { fact[n] = fact[n - 1] * n }   for b in 9...12 { print("The factorions for base \(b) are:")   for i in 1..<1500000 { var sum = 0 var j = i   while j > 0 { sum += fact[j % b] j /= b }   if sum == i { pr...
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#Vlang
Vlang
import strconv   fn main() { // cache factorials from 0 to 11 mut fact := [12]u64{} fact[0] = 1 for n := u64(1); n < 12; n++ { fact[n] = fact[n-1] * n }   for b := 9; b <= 12; b++ { println("The factorions for base $b are:") for i := u64(1); i < 1500000; i++ { ...
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#Wren
Wren
// cache factorials from 0 to 11 var fact = List.filled(12, 0) fact[0] = 1 for (n in 1..11) fact[n] = fact[n-1] * n   for (b in 9..12) { System.print("The factorions for base %(b) are:") for (i in 1...1500000) { var sum = 0 var j = i while (j > 0) { var d = j % b ...
http://rosettacode.org/wiki/Filter
Filter
Task Select certain elements from an Array into a new Array in a generic way. To demonstrate, select all even numbers from an Array. As an option, give a second solution which filters destructively, by modifying the original Array rather than creating a new Array.
#Visual_Basic_.NET
Visual Basic .NET
Module Filter   Sub Main() Dim array() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Dim newEvenArray() As Integer   Console.WriteLine("Current Array:") For Each i As Integer In array Console.WriteLine(i) Next   newEvenArray = filterArrayIntoNewArray(array)...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Prolog
Prolog
fizzbuzz :- foreach(between(1, 100, X), print_item(X)).   print_item(X) :- ( 0 is X mod 15 -> print('FizzBuzz') ; 0 is X mod 3 -> print('Fizz') ; 0 is X mod 5 -> print('Buzz') ; print(X) ), nl.
http://rosettacode.org/wiki/Extensible_prime_generator
Extensible prime generator
Task Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime. The routine should demonstrably rely on either: Being based on an open-ended counter set to count without upper limit other than system or programming language limits. In thi...
#EchoLisp
EchoLisp
  ; the first twenty primes (primes 20) → { 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 }   ; a stream to generate primes from a (define (primes-from a) (let ((p (next-prime a))) (stream-cons p (primes-from p))))   ; primes between 100,150 (for/list ((p (primes-from 100))) #:break (> p 150) ...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#Gecho
Gecho
0 1 dup wover + dup wover + dup wover + dup wover +
http://rosettacode.org/wiki/Factors_of_an_integer
Factors of an 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 |...
#Polyglot:PL.2FI_and_PL.2FM
Polyglot:PL/I and PL/M
factors_100H: procedure options (main);   /* PL/I DEFINITIONS */ %include 'pg.inc'; /* PL/M DEFINITIONS: CP/M BDOS SYSTEM CALL AND CONSOLE I/O ROUTINES, ETC. */ /* DECLARE BINARY LITERALLY 'ADDRESS', CHARAC...
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#ALGOL_68
ALGOL 68
# the increment-only accumulator # INT hq9accumulator := 0;   # interpret a HQ9+ code string # PROC hq9 = ( STRING code )VOID: FOR i TO UPB code DO CHAR op = code[ i ]; IF op = "Q" OR op = "q" THEN # display the program # print( ( code, newline ) ) ELIF ...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Action.21
Action!
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit   INT FUNC PowerI(INT base,exp) INT res,i   IF exp<0 THEN Break() FI   res=1 FOR i=1 TO exp DO res==*base OD RETURN (res)   PROC PowerR(REAL POINTER base INT exp REAL POINTER res) INT i REAL tmp   IF exp<0 THEN Break() FI   IntToReal(1...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#Ada
Ada
package Integer_Exponentiation is -- int^int procedure Exponentiate (Argument : in Integer; Exponent : in Natural; Result  : out Integer); function "**" (Left  : Integer; Right : Natural) return Integer;   -- real^int p...
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#Oz
Oz
declare Inf = 1.0e234 * 1.0e234 MinusInf = 1.0e234 * ~1.0e234 Zero = 1.0 / Inf MinusZero = 1.0 / MinusInf NaN = 0.0 / 0.0   {System.showInfo "infinite: "#Inf} {System.showInfo "-infinite: "#MinusInf} {System.showInfo "0: "#Zero} {System.showInfo "-0: "#MinusZero} %% seems to be identical to Zero {System.showInfo "NaN:...
http://rosettacode.org/wiki/Execute_SNUSP
Execute SNUSP
Execute SNUSP is an implementation of SNUSP. Other implementations of SNUSP. RCSNUSP SNUSP An implementation need only properly implement the Core SNUSP instructions ('$', '\', '/', '+', '-', '<', '>', ',', '.', '!', and '?'). Modular SNUSP ('#', '@') and Bloated SNUSP (':', ';', '%', and '&') are also allowed, but not...
#D
D
# # snusp.icn, A Modular SNUSP interpreter #   $define VERSION 0.6   # allow a couple of cli options link options   # directions $define DRIGHT 1 $define DLEFT 2 $define DUP 3 $define DDOWN 4   record position(row, col) global dir, ip, ram   procedure main(argv) local ch, codespace, col, dp, fn, line local r...
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#EchoLisp
EchoLisp
  (define-syntax-rule (if2 cond1 cond2 both cond1-only cond2-only none) ;; new syntax ;; will expand to : (if cond1 (if cond2 both cond1-only) (if cond2 cond2-only none))) → #syntax:if2   (define (num-test n) (if2 (positive? n) (exact? n) "positive and exact" "positive and inexact" "negative and...
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Groovy
Groovy
println(" 5 ** 3 ** 2 == " + 5**3**2) println("(5 ** 3)** 2 == " + (5**3)**2) println(" 5 **(3 ** 2)== " + 5**(3**2))
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Haskell
Haskell
λ> :i (^) (^) :: (Num a, Integral b) => a -> b -> a -- Defined in ‘GHC.Real’ infixr 8 ^ λ> :i (**) class Fractional a => Floating a where ... (**) :: a -> a -> a ... -- Defined in ‘GHC.Float’ infixr 8 ** λ> :i (^^) (^^) :: (Fractional a, Integral b) => a -> b -> a -- Defined in ‘GHC.Real’ infixr 8 ^^
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Io
Io
Io> 5**3**2 ==> 15625 Io> (5**3)**2 ==> 15625 Io> 5**(3**2) ==> 1953125 Io> 5 pow(3) pow(2) ==> 15625 Io> 5 **(3) **(2) ==> 15625 Io> Number getSlot("**") == Number getSlot("pow") ==> true Io>
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#VBScript
VBScript
' Factorions - VBScript - PG - 26/04/2020 Dim fact() nn1=9 : nn2=12 lim=1499999 ReDim fact(nn2) fact(0)=1 For i=1 To nn2 fact(i)=fact(i-1)*i Next For base=nn1 To nn2 list="" For i=1 To lim s=0 t=i Do While t<>0 d=t Mod base s=s+fact(d) t=t\base Loop If s=i Then list=list &...
http://rosettacode.org/wiki/Factorions
Factorions
Definition A factorion is a natural number that equals the sum of the factorials of its digits. Example 145   is a factorion in base 10 because: 1! + 4! + 5! = 1 + 24 + 120 = 145 It can be shown (see talk page) that no factorion in base 10 can exceed   1,499,999. Task Write a progr...
#zkl
zkl
var facts=[0..12].pump(List,fcn(n){ (1).reduce(n,fcn(N,n){ N*n },1) }); #(1,1,2,6....) fcn factorions(base){ fs:=List(); foreach n in ([1..1_499_999]){ sum,j := 0,n; while(j){ sum+=facts[j%base]; j/=base; } if(sum==n) fs.append(n); } fs }
http://rosettacode.org/wiki/Filter
Filter
Task Select certain elements from an Array into a new Array in a generic way. To demonstrate, select all even numbers from an Array. As an option, give a second solution which filters destructively, by modifying the original Array rather than creating a new Array.
#Vlang
Vlang
fn reduce(mut a []int){ mut last := 0 for e in a { if e%2==0 { a[last] = e last++ } } a = a[..last] } fn main() { mut nums := [5,4,8,2,4,6,5,6,34,12,21] even := nums.filter(it%2==0) println('orig: $nums') println('even: $even') reduce(mut nums)...
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#PureBasic
PureBasic
fun fizzbuzz(n :: NumPositive) -> String: doc: ```For positive input which is multiples of three return 'Fizz', for the multiples of five return 'Buzz'. For numbers which are multiples of both three and five return 'FizzBuzz'. Otherwise, return the number itself.``` ask: | num-modulo(n, 15) == 0 then: "FizzB...
http://rosettacode.org/wiki/Extensible_prime_generator
Extensible prime generator
Task Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime. The routine should demonstrably rely on either: Being based on an open-ended counter set to count without upper limit other than system or programming language limits. In thi...
#Elixir
Elixir
defmodule PrimesSoEMap do @typep stt :: {integer, integer, integer, Enumerable.integer, %{integer => integer}}   @spec advance(stt) :: stt defp advance {n, bp, q, bps?, map} do bps = if bps? === nil do Stream.drop(oddprms(), 1) else bps? end nn = n + 2 if nn >= q do inc = bp + bp nbps = bp...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#GFA_Basic
GFA Basic
  ' ' Compute nth Fibonacci number ' ' open a window for display OPENW 1 CLEARW 1 ' Display some fibonacci numbers ' Fib(46) is the largest number GFA Basic can reach ' (long integers are 4 bytes) FOR i%=0 TO 46 PRINT "fib(";i%;")=";@fib(i%) NEXT i% ' wait for a key press and tidy up ~INP(2) CLOSEW 1 ' ' Function to ...
http://rosettacode.org/wiki/Factors_of_an_integer
Factors of an 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 |...
#PowerShell
PowerShell
function Get-Factor ($a) { 1..$a | Where-Object { $a % $_ -eq 0 } }
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#ALGOL_W
ALGOL W
begin   procedure writeBottles( integer value bottleCount ) ; begin write( bottleCount, " bottle" ); if bottleCount not = 1 then writeon( "s " ) else writeon( " " ); end writeBottles ;   procedure hq9 ( string(32) value code  % code to execute %  ; integ...
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#Applesoft_BASIC
Applesoft BASIC
100 INPUT "HQ9+ : "; I$ 110 LET J$ = I$ + CHR$(13) 120 LET H$ = "HELLO, WORLD!" 130 LET B$ = "BOTTLES OF BEER" 140 LET W$ = " ON THE WALL" 150 LET W$ = W$ + CHR$(13) 160 FOR I = 1 TO LEN(I$) 170 LET C$ = MID$(J$, I, 1) 180 IF C$ = "H" THEN PRINT H$ 190 IF C$ = "Q" THEN PRINT I$ 200 LET A = A + (C$ = "+"...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#ALGOL_68
ALGOL 68
main:( INT two=2, thirty=30; # test constants # PROC VOID undefined;   # First implement exponentiation using a rather slow but sure FOR loop # PROC int pow = (INT base, exponent)INT: ( # PROC cannot be over loaded # IF exponent<0 THEN undefined FI; INT out:=( exponent=0 | 1 | base ); FROM 2 TO expone...
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#PARI.2FGP
PARI/GP
#!/usr/bin/perl use strict; use warnings;   my $nzero = -0.0; my $nan = 0 + "nan"; my $pinf = +"inf"; my $ninf = -"inf";   printf "\$nzero = %.1f\n", $nzero; print "\$nan = $nan\n"; print "\$pinf = $pinf\n"; print "\$ninf = $ninf\n\n";   printf "atan2(0, 0) = %g\n", atan2(0, 0); printf "atan2(0, \$nzero) = %g\n", atan2...
http://rosettacode.org/wiki/Execute_SNUSP
Execute SNUSP
Execute SNUSP is an implementation of SNUSP. Other implementations of SNUSP. RCSNUSP SNUSP An implementation need only properly implement the Core SNUSP instructions ('$', '\', '/', '+', '-', '<', '>', ',', '.', '!', and '?'). Modular SNUSP ('#', '@') and Bloated SNUSP (':', ';', '%', and '&') are also allowed, but not...
#F.23
F#
# # snusp.icn, A Modular SNUSP interpreter #   $define VERSION 0.6   # allow a couple of cli options link options   # directions $define DRIGHT 1 $define DLEFT 2 $define DUP 3 $define DDOWN 4   record position(row, col) global dir, ip, ram   procedure main(argv) local ch, codespace, col, dp, fn, line local r...
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#Emacs_Lisp
Emacs Lisp
  (defmacro if2 (cond1 cond2 both first second &rest neither) (let ((res1 (gensym)) (res2 (gensym))) `(let ((,res1 ,cond1) (,res2 ,cond2)) (cond ((and ,res1 ,res2) ,both) (,res1 ,first) (,res2 ,second) (t ,@ne...
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#J
J
5^3^2 1.95312e6 (5^3)^2 15625 5^(3^2) 1.95312e6
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Java
Java
jq -n 'pow(pow(5;3);2)' 15625
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#jq
jq
jq -n 'pow(pow(5;3);2)' 15625
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Julia
Julia
@show 5 ^ 3 ^ 2 # default: power operator is read right-to-left @show (5 ^ 3) ^ 2 @show 5 ^ (3 ^ 2) @show reduce(^, [5, 3, 2]) @show foldl(^, [5, 3, 2]) # guarantees left associativity @show foldr(^, [5, 3, 2]) # guarantees right associativity
http://rosettacode.org/wiki/Filter
Filter
Task Select certain elements from an Array into a new Array in a generic way. To demonstrate, select all even numbers from an Array. As an option, give a second solution which filters destructively, by modifying the original Array rather than creating a new Array.
#WDTE
WDTE
let a => import 'arrays'; let s => import 'stream';   a.stream [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] -> s.filter (@ even n => % n 2 -> == 0) -> s.collect -- io.writeln io.stdout ;
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Pyret
Pyret
fun fizzbuzz(n :: NumPositive) -> String: doc: ```For positive input which is multiples of three return 'Fizz', for the multiples of five return 'Buzz'. For numbers which are multiples of both three and five return 'FizzBuzz'. Otherwise, return the number itself.``` ask: | num-modulo(n, 15) == 0 then: "FizzB...
http://rosettacode.org/wiki/Extensible_prime_generator
Extensible prime generator
Task Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime. The routine should demonstrably rely on either: Being based on an open-ended counter set to count without upper limit other than system or programming language limits. In thi...
#F.23
F#
  let primeZ fN =primes()|>Seq.unfold(fun g-> Some(fN(g()), g)) let primesI() =primeZ bigint let primes64() =primeZ int64 let primes32() =primeZ int32 let pCache =Seq.cache(primes32()) let isPrime g=if g<2 then false else let mx=int(sqrt(float g)) in pCache|>Seq.takeWhile(fun n->n<=mx)|>Seq.forall(fun n->g%n>0...
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#GML
GML
///fibonacci(n) //Returns the nth fibonacci number   var n, numb; n = argument0;   if (n == 0) { numb = 0; } else { var fm2, fm1; fm2 = 0; fm1 = 1; numb = 1; repeat(n-1) { numb = fm2+fm1; fm2 = fm1; fm1 = numb; } }   return numb;
http://rosettacode.org/wiki/Factors_of_an_integer
Factors of an 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 |...
#ProDOS
ProDOS
editvar /newvar /value=a /userinput=1 /title=Enter an integer: do /delimspaces %% -a- >b printline Factors of -a-: -b-
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#Arturo
Arturo
hq9: function [source][ acc: 0 loop split source 'ch [ case [(lower ch)=] when? ["h"]-> print "Hello, world!" when? ["q"]-> print source when? ["9"]-> print "99 bottles here ..." when? ["+"]-> acc: acc+1 else []   ] return acc ]  ...
http://rosettacode.org/wiki/Execute_HQ9%2B
Execute HQ9+
Task Implement a   HQ9+   interpreter or compiler.
#AutoHotkey
AutoHotkey
; http://www.autohotkey.com/forum/viewtopic.php?p=356268#356268   testCode := "hq9+HqQ+Qq"   MsgBox % RunHQ9Plus(testCode)   ;---------------------------------   RunHQ9Plus(input) { Loop, Parse, input If ( A_LoopField = "+" ) acc++ Else If ( A_LoopField = "H" ) output .= "Hello, world!`n" Else...
http://rosettacode.org/wiki/Exponentiation_operator
Exponentiation operator
Most programming languages have a built-in implementation of exponentiation. Task Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an ov...
#AppleScript
AppleScript
on exponentiationOperatorTask(n, power) set power to power as integer set operatorResult to (n ^ power) set handlerResult to exponentiate(n, power)   return {operator:operatorResult, |handler|:handlerResult} end exponentiationOperatorTask   on exponentiate(n, power) -- AppleScript's ^ operator retur...
http://rosettacode.org/wiki/Extreme_floating_point_values
Extreme floating point values
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe ...
#Pascal
Pascal
#!/usr/bin/perl use strict; use warnings;   my $nzero = -0.0; my $nan = 0 + "nan"; my $pinf = +"inf"; my $ninf = -"inf";   printf "\$nzero = %.1f\n", $nzero; print "\$nan = $nan\n"; print "\$pinf = $pinf\n"; print "\$ninf = $ninf\n\n";   printf "atan2(0, 0) = %g\n", atan2(0, 0); printf "atan2(0, \$nzero) = %g\n", atan2...
http://rosettacode.org/wiki/Execute_SNUSP
Execute SNUSP
Execute SNUSP is an implementation of SNUSP. Other implementations of SNUSP. RCSNUSP SNUSP An implementation need only properly implement the Core SNUSP instructions ('$', '\', '/', '+', '-', '<', '>', ',', '.', '!', and '?'). Modular SNUSP ('#', '@') and Bloated SNUSP (':', ';', '%', and '&') are also allowed, but not...
#Factor
Factor
# # snusp.icn, A Modular SNUSP interpreter #   $define VERSION 0.6   # allow a couple of cli options link options   # directions $define DRIGHT 1 $define DLEFT 2 $define DUP 3 $define DDOWN 4   record position(row, col) global dir, ip, ram   procedure main(argv) local ch, codespace, col, dp, fn, line local r...
http://rosettacode.org/wiki/Extend_your_language
Extend your language
Control Structures These are examples of control structures. You may also be interested in: Conditional structures Exceptions Flow-control structures Loops Some programming languages allow you to extend the language. While this can be done to a certain degree in most languages (e.g. by using macros), other langua...
#F.23
F#
  // Extend your language. Nigel Galloway: September 14th., 2021 type elsetf=TF type elseft=FT type elseff=FF let elsetf,elseft,elseff=TF,FT,FF let if2 n g tt (TF:elsetf)tf (FT:elseft)ft (FF:elseff)ff=match(n,g) with (true,true)->tt() |(true,false)->tf() |(false,true)->ft() |_->ff() if2 (13<23) (23<42) (fun()->printfn ...
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Kotlin
Kotlin
// version 1.0.5-2   infix fun Int.ipow(exp: Int): Int = when { exp < 0 -> throw IllegalArgumentException("negative exponents not allowed") exp == 0 -> 1 else -> { var ans = 1 var base = this var e = exp while(e != 0) { if (e and 1 == 1) ans *= base ...
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Lambdatalk
Lambdatalk
  '{pow {pow 5 3} 2} -> {pow {pow 5 3} 2} '{pow 5 {pow 3 2}} -> {pow 5 {pow 3 2}}  
http://rosettacode.org/wiki/Exponentiation_order
Exponentiation order
This task will demonstrate the order of exponentiation   (xy)   when there are multiple exponents. (Many programming languages,   especially those with extended─precision integer arithmetic,   usually support one of **, ^, ↑ or some such for exponentiation.) Task requirements Show the result of a language's eval...
#Latitude
Latitude
5 ^ 3 ^ 2.  ;; 1953125 (5 ^ 3) ^ 2. ;; 15625 5 ^ (3 ^ 2). ;; 1953125
http://rosettacode.org/wiki/Filter
Filter
Task Select certain elements from an Array into a new Array in a generic way. To demonstrate, select all even numbers from an Array. As an option, give a second solution which filters destructively, by modifying the original Array rather than creating a new Array.
#Wrapl
Wrapl
VAR a <- ALL 1:to(10);
http://rosettacode.org/wiki/FizzBuzz
FizzBuzz
Task Write a program that prints the integers from   1   to   100   (inclusive). But:   for multiples of three,   print   Fizz     (instead of the number)   for multiples of five,   print   Buzz     (instead of the number)   for multiples of both three and five,   print   FizzBuzz     (instead of the number) ...
#Python
Python
for i in xrange(1, 101): if i % 15 == 0: print "FizzBuzz" elif i % 3 == 0: print "Fizz" elif i % 5 == 0: print "Buzz" else: print i
http://rosettacode.org/wiki/Extensible_prime_generator
Extensible prime generator
Task Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime. The routine should demonstrably rely on either: Being based on an open-ended counter set to count without upper limit other than system or programming language limits. In thi...
#Factor
Factor
USING: io math.primes prettyprint sequences ;   "First 20 primes: " write 20 nprimes .   "Primes between 100 and 150: " write 100 150 primes-between .   "Number of primes between 7,700 and 8,000: " write 7,700 8,000 primes-between length .   "10,000th prime: " write 10,000 nprimes last .
http://rosettacode.org/wiki/Fibonacci_sequence
Fibonacci sequence
The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2, if n>1 Task Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow ...
#Go
Go
func fib(a int) int { if a < 2 { return a } return fib(a - 1) + fib(a - 2) }
http://rosettacode.org/wiki/Factors_of_an_integer
Factors of an 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 |...
#Prolog
Prolog
  brute_force_factors( N , Fs ) :- integer(N) , N > 0 , setof( F , ( between(1,N,F) , N mod F =:= 0 ) , Fs ) .