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 |... | #Perl | Perl | sub factors
{
my($n) = @_;
return grep { $n % $_ == 0 }(1 .. $n);
}
print join ' ',factors(64), "\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 ... | #Go | Go | package main
import (
"fmt"
"math"
)
func main() {
// compute "extreme values" from non-extreme values
var zero float64 // zero is handy.
var negZero, posInf, negInf, nan float64 // values to compute.
negZero = zero * -1
posInf = 1 / zero
negInf = -1 / zero
... |
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... | #Agda | Agda |
data Bool : Set where
true : Bool
false : Bool
if_then_else : ∀ {l} {A : Set l} -> Bool -> A -> A -> A
if true then t else e = t
if false then t else e = e
if2_,_then_else1_else2_else_ : ∀ {l} {A : Set l} -> (b1 b2 : Bool) -> (t e1 e2 e : A) -> A
if2 true , true then t else1 e1 else2 e2 else e = t
if2 true ... |
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... | #ALGOL_68 | ALGOL 68 | # operator to turn two boolean values into an integer - name inspired by the COBOL sample #
PRIO ALSO = 1;
OP ALSO = ( BOOL a, b )INT: IF a AND b THEN 1 ELIF a THEN 2 ELIF b THEN 3 ELSE 4 FI;
# using the above operator, we can use the standard CASE construct to provide the #
# required construct, e.g.: ... |
http://rosettacode.org/wiki/Factorial_base_numbers_indexing_permutations_of_a_collection | Factorial base numbers indexing permutations of a collection | You need a random arrangement of a deck of cards, you are sick of lame ways of doing this. This task is a super-cool way of doing this using factorial base numbers.
The first 25 factorial base numbers in increasing order are: 0.0.0, 0.0.1, 0.1.0, 0.1.1, 0.2.0, 0.2.1, 1.0.0, 1.0.1, 1.1.0, 1.1.1,1.2.0, 1.2.1, 2.0.0, 2.0.... | #Wren | Wren | import "random" for Random
import "/math" for Int
import "/fmt" for Fmt
var genFactBaseNums = Fn.new { |size, countOnly|
var results = []
var count = 0
var n = 0
while (true) {
var radix = 2
var res = null
if (!countOnly) res = List.filled(size, 0)
var k = n
wh... |
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... | #Go | Go | package main
import (
"fmt"
"strconv"
)
func main() {
// cache factorials from 0 to 11
var fact [12]uint64
fact[0] = 1
for n := uint64(1); n < 12; n++ {
fact[n] = fact[n-1] * n
}
for b := 9; b <= 12; b++ {
fmt.Printf("The factorions for base %d are:\n", b)
f... |
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... | #Haskell | Haskell | import Text.Printf (printf)
import Data.List (unfoldr)
import Control.Monad (guard)
factorion :: Int -> Int -> Bool
factorion b n = f b n == n
where
f b = sum . map (product . enumFromTo 1) . unfoldr (\x -> guard (x > 0) >> pure (x `mod` b, x `div` b))
main :: IO ()
main = mapM_ (uncurry (printf "Factorions for ... |
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.
| #Tcl | Tcl | set l {56 21 71 27 39 62 87 76 82 94 45 83 65 45 28 90 52 44 1 89}
puts [lmap x $l {if {$x % 2} continue; set x}] |
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)
... | #PL.2FI | PL/I | do i = 1 to 100;
select;
when (mod(i,15) = 0) put skip list ('FizzBuzz');
when (mod(i,3) = 0) put skip list ('Fizz');
when (mod(i,5) = 0) put skip list ('Buzz');
otherwise put skip list (i);
end;
end; |
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... | #AutoHotkey | AutoHotkey | SetBatchLines, -1
p := 1 ;p functions as the counter
Loop, 10000 {
p := NextPrime(p)
if (A_Index < 21)
a .= p ", "
if (p < 151 && p > 99)
b .= p ", "
if (p < 8001 && p > 7699)
c++
}
MsgBox, % "First twenty primes: " RTrim(a, ", ")
. "`nPrimes between 100 and 150: " RTrim(b, ", ")
. "`nNumber of primes betwe... |
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 ... | #Free_Pascal | Free Pascal | type
/// domain for Fibonacci function
/// where result is within nativeUInt
// You can not name it fibonacciDomain,
// since the Fibonacci function itself
// is defined for all whole numbers
// but the result beyond F(n) exceeds high(nativeUInt).
fibonacciLeftInverseRange =
{$ifdef CPU64} 0..93 {$else} 0..47 ... |
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 |... | #Phix | Phix | ?factors(12345,1)
|
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 ... | #Groovy | Groovy | def negInf = -1.0d / 0.0d; //also Double.NEGATIVE_INFINITY
def inf = 1.0d / 0.0d; //also Double.POSITIVE_INFINITY
def nan = 0.0d / 0.0d; //also Double.NaN
def negZero = -2.0d / inf;
println(" Negative inf: " + negInf);
println(" Positive inf: " + inf);
println(" NaN: " + nan);
println(" 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 ... | #haskell | haskell |
main = do
let inf = 1/0
let minus_inf = -1/0
let minus_zero = -1/inf
let nan = 0/0
putStrLn ("Positive infinity = "++(show inf))
putStrLn ("Negative infinity = "++(show minus_inf))
putStrLn ("Negative zero = "++(show minus_zero))
putStrLn ("Not a number = "++(show nan))
--Some Arithmetic
putStrLn ("inf + 2.0 = ... |
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... | #ALGOL_W | ALGOL W | begin
% executes pBoth, p1, p2 or pNeither %
% depending on whether c1 and c2 are true, c1 is true, c2 is true %
% neither c1 nor c2 are true %
procedure if2 ( logical value c1, c2
; procedure ... |
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... | #Arturo | Arturo | if2: function [cond1 cond2 both one two none][
case []
when? [and? cond1 cond2] -> do both
when? [cond1] -> do one
when? [cond2] -> do two
else -> do none
]
if2 false true [print "both"]
[print "only first"]
[print "only second"]
[print "none"] |
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... | #11l | 11l | print(5 ^ 3 ^ 2)
print((5 ^ 3) ^ 2)
print(5 ^ (3 ^ 2)) |
http://rosettacode.org/wiki/Factorial_base_numbers_indexing_permutations_of_a_collection | Factorial base numbers indexing permutations of a collection | You need a random arrangement of a deck of cards, you are sick of lame ways of doing this. This task is a super-cool way of doing this using factorial base numbers.
The first 25 factorial base numbers in increasing order are: 0.0.0, 0.0.1, 0.1.0, 0.1.1, 0.2.0, 0.2.1, 1.0.0, 1.0.1, 1.1.0, 1.1.1,1.2.0, 1.2.1, 2.0.0, 2.0.... | #zkl | zkl | fcn fpermute(omega,num){ // eg (0,1,2,3), (0,0,0)..(3,2,1)
omega=omega.copy(); // omega gonna be mutated
foreach m,g in ([0..].zip(num)){ if(g) omega.insert(m,omega.pop(m+g)) }
omega
} |
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... | #J | J |
index=: $ #: I.@:,
factorion=: 10&$: :(] = [: +/ [: ! #.^:_1)&>
FACTORIONS=: 9 0 +"1 index Q=: 9 10 11 12 factorion/ i. 1500000
NB. base, factorion expressed in bases 10, and base
(,. ".@:((Num_j_,26}.Alpha_j_) {~ #.inv/)"1) FACTORIONS
9 1 1
9 2 2
9 41282 62558
10 1 1
10 ... |
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... | #Java | Java |
public class Factorion {
public static void main(String [] args){
System.out.println("Base 9:");
for(int i = 1; i <= 1499999; i++){
String iStri = String.valueOf(i);
int multiplied = operate(iStri,9);
if(multiplied == i){
System.out.print(i + "\t... |
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.
| #Toka | Toka | 10 cells is-array table
10 cells is-array even
{
variable source
[ swap source ! >r reset r> 0
[ i source @ array.get
dup 2 mod 0 <> [ drop ] ifTrue
] countedLoop
depth 0 swap [ i even array.put ] countedLoop
]
} is copy-even
10 0 [ i i table array.put ] countedLoop
table 10 copy-even |
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)
... | #PL.2FM | PL/M | 100H:
/* DECLARE OUTPUT IN TERMS OF CP/M -
PL/M DOES NOT COME WITH ANY STANDARD LIBRARY */
BDOS: PROCEDURE(FUNC, ARG);
DECLARE FUNC BYTE, ARG ADDRESS;
GO TO 5;
END BDOS;
PUT$STRING: PROCEDURE(STR);
DECLARE STR ADDRESS;
CALL BDOS(9, STR);
CALL BDOS(9, .(13,10,'$'));
END PUT$STRING;
/* PRINT... |
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... | #BQN | BQN | # Function that returns a new prime generator
PrimeGen ← {𝕤
i ← 0 # Counter: index of next prime to be output
primes ← ↕0
next ← 2
Sieve ← { p 𝕊 i‿n:
E ← {↕∘⌈⌾(((𝕩|-i)+𝕩×⊢)⁼)n-i} # Indices of multiples of 𝕩
i + / (1⥊˜n-i) E⊸{0¨⌾(𝕨⊸⊏)𝕩}´ p # Primes in segment [i,n)
}
{𝕤
{ i=≠pri... |
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 ... | #FreeBASIC | FreeBASIC | 'Fibonacci extended
'Freebasic version 24 Windows
Dim Shared ADDQmod(0 To 19) As Ubyte
Dim Shared ADDbool(0 To 19) As Ubyte
For z As Integer=0 To 19
ADDQmod(z)=(z Mod 10+48)
ADDbool(z)=(-(10<=z))
Next z
Function plusINT(NUM1 As String,NUM2 As String) As String
Dim As Byte flag
#macro finish()
... |
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 |... | #Phixmonti | Phixmonti | /# Rosetta Code problem: http://rosettacode.org/wiki/Factors_of_an_integer
by Galileo, 05/2022 #/
include ..\Utilitys.pmt
def Factors >ps
( ( 1 tps 2 / ) for tps over mod if drop endif endfor ps> )
enddef
11 Factors
21 Factors
32 factors
45 factors
67 factors
96 factors
pstack |
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 ... | #Icon_and_Unicon | Icon and Unicon | Inf=: _
NegInf=: __
NB. Negative zero cannot be represented in J to be distinct from 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 ... | #J | J | Inf=: _
NegInf=: __
NB. Negative zero cannot be represented in J to be distinct from 0.
NaN=. _. |
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... | #ATS | ATS | (* Languages with pattern matching ALREADY HAVE THIS! *)
fn
func (pred1 : bool, pred2 : bool) : void =
case+ (pred1, pred2) of
| (true, true) => println! ("(true, true)")
| (true, false) => println! ("(true, false)")
| (false, true) => println! ("(false, true)")
| (false, false) => println! ("(false, 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... | #C | C | /* Four-way branch.
*
* if2 (firsttest, secondtest
* , bothtrue
* , firstrue
* , secondtrue
* , bothfalse
* )
*/
#define if2(firsttest,secondtest,bothtrue,firsttrue,secondtrue,bothfalse)\
switch(((firsttest)?0:2)+((secondtest)?0:1)) {\
case 0: bothtrue; break;\
case 1: firsttrue; break;\
case 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... | #Action.21 | Action! | INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
PROC Main()
REAL r2,r3,r5,tmp1,tmp2
Put(125) PutE() ;clear screen
IntToReal(2,r2)
IntToReal(3,r3)
IntToReal(5,r5)
PrintE("There is no power operator in Action!")
PrintE("Power function for REAL type is used.")
PrintE("But the precision is insuffic... |
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... | #Ada | Ada | with Ada.Text_IO;
procedure Exponentation_Order is
use Ada.Text_IO;
begin
-- Put_Line ("5**3**2 : " & Natural'(5**3**2)'Image);
Put_Line ("(5**3)**2 : " & Natural'((5**3)**2)'Image);
Put_Line ("5**(3**2) : " & Natural'(5**(3**2))'Image);
end Exponentation_Order; |
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... | #ALGOL_68 | ALGOL 68 | print( ( "5**3**2: ", 5**3**2, newline ) );
print( ( "(5**3)**2: ", (5**3)**2, newline ) );
print( ( "5**(3**2): ", 5**(3**2), newline ) ) |
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... | #Julia | Julia | isfactorian(n, base) = mapreduce(factorial, +, map(c -> parse(Int, c, base=16), split(string(n, base=base), ""))) == n
printallfactorian(base) = println("Factorians for base $base: ", [n for n in 1:100000 if isfactorian(n, base)])
foreach(printallfactorian, 9: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... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | ClearAll[FactorionQ]
FactorionQ[n_,b_:10]:=Total[IntegerDigits[n,b]!]==n
Select[Range[1500000],FactorionQ[#,9]&]
Select[Range[1500000],FactorionQ[#,10]&]
Select[Range[1500000],FactorionQ[#,11]&]
Select[Range[1500000],FactorionQ[#,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... | #Nim | Nim | from math import fac
from strutils import join
iterator digits(n, base: Natural): Natural =
## Yield the digits of "n" in base "base".
var n = n
while true:
yield n mod base
n = n div base
if n == 0: break
func isFactorion(n, base: Natural): bool =
## Return true if "n" is a factorion for base "... |
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.
| #TUSCRIPT | TUSCRIPT |
$$ MODE TUSCRIPT
arr="1'4'9'16'25'36'49'64'81'100",even=""
LOOP nr=arr
rest=MOD (nr,2)
IF (rest==0) even=APPEND (even,nr)
ENDLOOP
PRINT even
|
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)
... | #PL.2FSQL | PL/SQL | BEGIN
FOR i IN 1 .. 100
LOOP
CASE
WHEN MOD(i, 15) = 0 THEN
DBMS_OUTPUT.put_line('FizzBuzz');
WHEN MOD(i, 5) = 0 THEN
DBMS_OUTPUT.put_line('Buzz');
WHEN MOD(i, 3) = 0 THEN
DBMS_OUTPUT.put_line('Fizz');
ELSE
DBMS_OUTPUT.put_line(i);
END CASE;
END LOOP;
END; |
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... | #C | C | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define CHUNK_BYTES (32 << 8)
#define CHUNK_SIZE (CHUNK_BYTES << 6)
int field[CHUNK_BYTES];
#define GET(x) (field[(x)>>6] & 1<<((x)>>1&31))
#define SET(x) (field[(x)>>6] |= 1<<((x)>>1&31))
typedef unsigned uint;
typedef struct {
... |
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 ... | #Frink | Frink |
fibonacciN[n] :=
{
a = 0
b = 1
count = 0
while count < n
{
[a,b] = [b, a + b]
count = count + 1
}
return a
}
|
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 |... | #PHP | PHP | function GetFactors($n){
$factors = array(1, $n);
for($i = 2; $i * $i <= $n; $i++){
if($n % $i == 0){
$factors[] = $i;
if($i * $i != $n)
$factors[] = $n/$i;
}
}
sort($factors);
return $factors;
} |
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... | #ALGOL_68 | ALGOL 68 | BEGIN # execute some ComputerZero programs #
# instructions #
INT nop = 0, lda = 1, sta = 2, add = 3, sub = 4, brz = 5, jmp = 6, stp = 7;
PROC instr = ( INT op, v )INT: ( 32 * op ) + v;
OP NOP = ( INT v )... |
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 ... | #Java | Java | public class Extreme {
public static void main(String[] args) {
double negInf = -1.0 / 0.0; //also Double.NEGATIVE_INFINITY
double inf = 1.0 / 0.0; //also Double.POSITIVE_INFINITY
double nan = 0.0 / 0.0; //also Double.NaN
double negZero = -2.0 / inf;
System.out.println("Neg... |
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... | #11l | 11l | V HW = ‘
/++++!/===========?\>++.>+.+++++++..+++\
\+++\ | /+>+++++++>/ /++++++++++<<.++>./
$+++/ | \+++++++++>\ \+++++.>.+++.-----\
\==-<<<<+>+++/ /=.>.+>.--------.-/’
F snusp(store, code)
V ds = [Byte(0)] * store
V dp = 0
V cs = code.split("\n")
V ipr = 0
V ipc = 0
L(row) cs
ipc = row... |
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... | #C.23 | C# |
using System;
using System.Reflection;
namespace Extend_your_language
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine("Hello World!");
Console.WriteLine();
int x = 0;
int y = 0;
for(x=0;x<2;x++)
{
for(y=0;y<2;y++)
{
... |
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... | #ALGOL_W | ALGOL W | begin
write( "5**3**2: ", round( 5 ** 3 ** 2 ) );
write( "(5**3)**2: ", round( ( 5 ** 3 ) ** 2 ) );
write( "5**(3**2): ", round( 5 ** round( 3 ** 2 ) ) )
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... | #APL | APL | 5*3*2
1953125
(5*3)*2
15625
5*(3*2)
1953125
|
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... | #AppleScript | AppleScript | set r1 to 5 ^ 3 ^ 2 -- Changes to 5 ^ (3 ^ 2) when compiled.
set r2 to (5 ^ 3) ^ 2
set r3 to 5 ^ (3 ^ 2)
return "5 ^ 3 ^ 2 = " & r1 & "
(5 ^ 3) ^ 2 = " & r2 & "
5 ^ (3 ^ 2) = " & r3 |
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... | #OCaml | OCaml | let () =
(* cache factorials from 0 to 11 *)
let fact = Array.make 12 0 in
fact.(0) <- 1;
for n = 1 to pred 12 do
fact.(n) <- fact.(n-1) * n;
done;
for b = 9 to 12 do
Printf.printf "The factorions for base %d are:\n" b;
for i = 1 to pred 1_500_000 do
let sum = ref 0 in
let j = ref ... |
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... | #Pascal | Pascal | program munchhausennumber;
{$IFDEF FPC}{$MODE objFPC}{$Optimization,On,all}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
type
tdigit = byte;
const
MAXBASE = 17;
var
DgtPotDgt : array[0..MAXBASE-1] of NativeUint;
dgtCnt : array[0..MAXBASE-1] of NativeInt;
cnt: NativeUint;
function convertToString(n:N... |
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.
| #UNIX_Shell | UNIX Shell | a=(1 2 3 4 5)
unset e[@]
for ((i=0;i<${#a[@]};i++)); do
[ $((a[$i]%2)) -eq 0 ] && e[$i]="${a[$i]}"
done |
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)
... | #Pony | Pony | use "collections"
actor Main
new create(env: Env) =>
for i in Range[I32](1, 100) do
env.out.print(fizzbuzz(i))
end
fun fizzbuzz(n: I32): String =>
if (n % 15) == 0 then
"FizzBuzz"
elseif (n % 5) == 0 then
"Buzz"
elseif (n % 3) == 0 then
"Fizz"
else
n.string(... |
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... | #C.2B.2B | C++ | #include <iostream>
#include <cstdint>
#include <queue>
#include <utility>
#include <vector>
#include <limits>
template<typename integer>
class prime_generator {
public:
integer next_prime();
integer count() const {
return count_;
}
private:
struct queue_item {
queue_item(integer prime... |
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 ... | #FRISC_Assembly | FRISC Assembly | FIBONACCI PUSH R1
PUSH R2
PUSH R3
MOVE 0, R1
MOVE 1, R2
FIB_LOOP SUB R0, 1, R0
JP_Z FIB_DONE
MOVE R2, R3
ADD R1, R2, R2
MOVE R3, R1
JP FIB_LOOP
FIB_DONE MOVE R2, R... |
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 |... | #Picat | Picat | factors(N) = [[D,N // D] : D in 1..N.sqrt.floor, N mod D == 0].flatten.sort_remove_dups. |
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... | #J | J | OPS=: 'nop lda sta add sub brz jmp stp'
assemble1=: {{
y=. tolower y
ins=. {.;:y-.":i.10
cod=. 8|(;:OPS) i. ins
val=. {.0".y-.OPS
if. *cod do.
assert. 32>val
val+32*cod
else.
assert. 256>val
val
end.
}}
assemble=: {{
if. 0=L. y do.
delim=. {.((tolower y)-.(":i.10),;OPS),LF
y... |
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... | #Julia | Julia | mutable struct ComputerZero
ip::Int
ram::Vector{UInt8}
accum::UInt8
isready::Bool
end
function ComputerZero(program)
memory = zeros(UInt8, 32)
for i in 1:min(32, length(program))
memory[i] = program[i]
end
return ComputerZero(1, memory, 0, true)
end
NOP(c) = (c.ip = mod1(c.ip +... |
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 ... | #jq | jq | 0/0 #=> null
1e1000 #=> 1.7976931348623157e+308 |
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 ... | #Julia | Julia |
function showextremes()
values = [0.0, -0.0, Inf, -Inf, NaN]
println(1 ./ values)
end
showextremes()
@show Inf + 2.0
@show Inf + Inf
@show Inf - Inf
@show Inf * Inf
@show Inf / Inf
@show Inf * 0
@show 0 == -0
@show NaN == NaN
@show NaN === 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... | #Ada | Ada | #
# 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... | #Clay | Clay | alias if2(cond1:Bool,
cond2:Bool,
both,
first,
second,
neither)
{
var res1 = cond1;
var res2 = cond2;
if (res1 and res2) return both;
if (res1) return first;
if (res2) return second;
return neither;
}
|
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... | #Clojure | Clojure |
(defmacro if2 [[cond1 cond2] bothTrue firstTrue secondTrue else]
`(let [cond1# ~cond1
cond2# ~cond2]
(if cond1# (if cond2# ~bothTrue ~firstTrue)
(if cond2# ~secondTrue ~else))))
|
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... | #Arturo | Arturo | print 5^3^2
print (5^3)^2
print 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... | #AWK | AWK |
# syntax: GAWK -f EXPONENTIATION_ORDER.AWK
BEGIN {
printf("5^3^2 = %d\n",5^3^2)
printf("(5^3)^2 = %d\n",(5^3)^2)
printf("5^(3^2) = %d\n",5^(3^2))
exit(0)
}
|
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... | #BASIC | BASIC | 10 PRINT "5**3**2 = ";5**3**2
20 PRINT "(5**3)**2 = ";(5**3)**2
30 PRINT "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... | #Perl | Perl | use strict;
use warnings;
use ntheory qw/factorial todigits/;
my $limit = 1500000;
for my $b (9 .. 12) {
print "Factorions in base $b:\n";
$_ == factorial($_) and print "$_ " for 0..$b-1;
for my $i (1 .. int $limit/$b) {
my $sum;
my $prod = $i * $b;
for (reverse todigits($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... | #Phix | Phix | with javascript_semantics
for base=9 to 12 do
printf(1,"The factorions for base %d are: ", base)
for i=1 to 1499999 do
atom total = 0, j = i, d
while j>0 and total<=i do
d = remainder(j,base)
total += factorial(d)
j = floor(j/base)
end while
if... |
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.
| #UnixPipes | UnixPipes | yes \ | cat -n | while read a; do ; expr $a % 2 >/dev/null && echo $a ; done |
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)
... | #Pop11 | Pop11 | lvars str;
for i from 1 to 100 do
if i rem 15 = 0 then
'FizzBuzz' -> str;
elseif i rem 3 = 0 then
'Fizz' -> str;
elseif i rem 5 = 0 then
'Buzz' -> str;
else
'' >< i -> str;
endif;
printf(str, '%s\n');
endfor; |
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... | #Clojure | Clojure | ns test-project-intellij.core
(:gen-class)
(:require [clojure.string :as string]))
(def primes
" The following routine produces a infinite sequence of primes
(i.e. can be infinite since the evaluation is lazy in that it
only produces values as needed). The method is from clojure primes.clj library
which ... |
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 ... | #FunL | FunL | def
fib( 0 ) = 0
fib( 1 ) = 1
fib( n ) = fib( n - 1 ) + fib( n - 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 |... | #PicoLisp | PicoLisp | (de factors (N)
(filter
'((D) (=0 (% N D)))
(range 1 N) ) ) |
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... | #Phix | Phix | with javascript_semantics
constant NOP = 0b000_00000, -- no operation
LDA = 0b001_00000, -- load accumulator, a := memory [xxxxx]
STA = 0b010_00000, -- store accumulator, memory [xxxxx] := a
ADD = 0b011_00000, -- add, a := a + memory [xxxxx]
SUB = 0b100_00000, -- subtract, a := a – m... |
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 ... | #Kotlin | Kotlin | // version 1.0.5-2
@Suppress("DIVISION_BY_ZERO", "FLOAT_LITERAL_CONFORMS_ZERO")
fun main(args: Array<String>) {
val inf = 1.0 / 0.0
val negInf = -1.0 / 0.0
val nan = 0.0 / 0.0
val negZero = -1.0e-325
println("*** Indirect ***\n")
println("Infinity : $inf")
println(... |
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 ... | #Lua | Lua |
local inf=math.huge
local minusInf=-math.huge
local NaN=0/0
local negativeZeroSorta=-1E-240
|
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... | #ALGOL_68 | ALGOL 68 | #
# 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... | #COBOL | COBOL |
EVALUATE EXPRESSION-1 ALSO EXPRESSION-2
WHEN TRUE ALSO TRUE
DISPLAY 'Both are true.'
WHEN TRUE ALSO FALSE
DISPLAY 'Expression 1 is true.'
WHEN FALSE ALSO TRUE
DISPLAY 'Expression 2 is true.'
WHEN OTHER
DISPLAY 'Neither is true.'
END-EVALUATE
|
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... | #Bracmat | Bracmat | put$str$("5^3^2: " 5^3^2 "\n(5^3)^2: " (5^3)^2 "\n5^(3^2): " 5^(3^2) \n) |
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... | #C | C | #include<stdio.h>
#include<math.h>
int main()
{
printf("(5 ^ 3) ^ 2 = %.0f",pow(pow(5,3),2));
printf("\n5 ^ (3 ^ 2) = %.0f",pow(5,pow(3,2)));
return 0;
} |
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... | #C.2B.2B | C++ | #include <iostream>
#include <cmath>
int main() {
std::cout << "(5 ^ 3) ^ 2 = " << (uint) pow(pow(5,3), 2) << std::endl;
std::cout << "5 ^ (3 ^ 2) = "<< (uint) pow(5, (pow(3, 2)));
return EXIT_SUCCESS;
} |
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... | #PureBasic | PureBasic | Declare main()
If OpenConsole() : main() : Else : End 1 : EndIf
Input() : End
Procedure main()
Define.i n,b,d,i,j,sum
Dim fact.i(12)
fact(0)=1
For n=1 To 11 : fact(n)=fact(n-1)*n : Next
For b=9 To 12
PrintN("The factorions for base "+Str(b)+" are: ")
For i=1 To 1500000-1
sum=0 : j=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... | #Python | Python | fact = [1] # cache factorials from 0 to 11
for n in range(1, 12):
fact.append(fact[n-1] * n)
for b in range(9, 12+1):
print(f"The factorions for base {b} are:")
for i in range(1, 1500000):
fact_sum = 0
j = i
while j > 0:
d = j % b
fact_sum += fact[d]
... |
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.
| #Ursala | Ursala | #import std
#import nat
x = <89,36,13,15,41,39,21,3,15,92,16,59,52,88,33,65,54,88,93,43>
#cast %nL
y = (not remainder\2)*~ x |
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)
... | #PostScript | PostScript | 1 1 100 {
/c false def
dup 3 mod 0 eq { (Fizz) print /c true def } if
dup 5 mod 0 eq { (Buzz) print /c true def } if
c {pop}{( ) cvs print} ifelse
(\n) print
} for |
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... | #CoffeeScript | CoffeeScript |
primes = () ->
yield 2
yield 3
sieve = ([] for i in [1..3])
sieve[0].push 3
[r, s] = [3, 9]
pos = 1
n = 5
loop
isPrime = true
if sieve[pos].length > 0 # this entry has a list of factors
isPrime = false
sieve[(pos + m) % sieve.length].push m fo... |
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 ... | #Futhark | Futhark |
fun main(n: int): int =
loop((a,b) = (0,1)) = for _i < n do
(b, a + b)
in a
|
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 |... | #PILOT | PILOT | T :Enter a number.
A :#n
C :factor = 1
T :The factors of #n are:
*Loop
C :remainder = n % factor
T ( remainder = 0 ) :#factor
J ( factor = n ) :*Finished
C :factor = factor + 1
J :*Loop
*Finished
END: |
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... | #Python | Python | """Computer/zero Assembly emulator. Requires Python >= 3.7"""
import re
from typing import Dict
from typing import Iterable
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import Tuple
NOP = 0b000
LDA = 0b001
STA = 0b010
ADD = 0b011
SUB = 0b100
BRZ = 0b101
JMP = 0b1... |
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 ... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | Column@{ReleaseHold[
Function[expression,
Row@{HoldForm@InputForm@expression, " = ", Quiet@expression},
HoldAll] /@
Hold[1./0., 0./0., Limit[-Log[x], x -> 0], Limit[Log[x], x -> 0],
Infinity + 1, Infinity + Infinity, 2 Infinity,
Infinity - Infinity, 0 Infinity, ComplexInfinity + 1,
... |
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 ... | #Maxima | Maxima | USER>write 3e145
30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
USER>write 3e146
<MAXNUMBER>
|
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... | #AutoHotkey | AutoHotkey | #
# 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... | #Common_Lisp | Common 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 ,@neit... |
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... | #C.23 | C# | using System;
namespace exponents
{
class Program
{
static void Main(string[] args)
{
/*
* Like C, C# does not have an exponent operator.
* Exponentiation is done via Math.Pow, which
* only takes two arguments
*/
Con... |
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... | #Clojure | Clojure | (use 'clojure.math.numeric-tower)
;; (5**3)**2
(expt (expt 5 3) 2) ; => 15625
;; 5**(3**2)
(expt 5 (expt 3 2)) ; => 1953125
;; (5**3)**2 alternative: use reduce
(reduce expt [5 3 2]) ; => 15625
;; 5**(3**2) alternative: evaluating right-to-left with reduce requires a small modification
(defn rreduce [... |
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... | #Quackery | Quackery | [ table ] is results ( n --> s )
4 times
[ ' [ stack [ ] ]
copy
' results put ]
[ results dup take
rot join swap put ] is addresult ( n n --> )
[ table 9 10 11 12 ] is radix ( n --> n )
[ table 1 ] is ! ( n --> n )
1 11 times
... |
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... | #Racket | Racket | #lang racket
(define fact
(curry list-ref (for/fold ([result (list 1)] #:result (reverse result))
([x (in-range 1 20)])
(cons (* x (first result)) result))))
(for ([b (in-range 9 13)])
(printf "The factorions for base ~a are:\n" b)
(for ([i (in-range 1 1500000)]... |
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... | #Raku | Raku | constant @factorial = 1, |[\*] 1..*;
constant $limit = 1500000;
constant $bases = 9 .. 12;
my @result;
$bases.map: -> $base {
@result[$base] = "\nFactorions in base $base:\n1 2";
sink (1 .. $limit div $base).map: -> $i {
my $product = $i * $base;
my $partial;
for $i.polymod... |
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.
| #V | V | [even? dup 2 / >int 2 * - zero?].
[1 2 3 4 5 6 7 8 9] [even?] filter
=[2 4 6 8] |
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)
... | #Potion | Potion |
1 to 100 (a):
if (a % 15 == 0):
'FizzBuzz'.
elsif (a % 3 == 0):
'Fizz'.
elsif (a % 5 == 0):
'Buzz'.
else: a. string print
"\n" print. |
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... | #D | D | void main() {
import std.stdio, std.range, std.algorithm, sieve_of_eratosthenes3;
Prime prime;
writeln("First twenty primes:\n", 20.iota.map!prime);
writeln("Primes primes between 100 and 150:\n",
uint.max.iota.map!prime.until!q{a > 150}.filter!q{a > 99});
writeln("Number of primes bet... |
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 ... | #FutureBasic | FutureBasic | window 1, @"Fibonacci Sequence", (0,0,480,620)
local fn Fibonacci( n as long ) as long
static long s1
static long s2
long temp
if ( n < 2 )
s1 = n
exit fn
else
temp = s1 + s2
s2 = s1
s1 = temp
exit fn
end if
end fn = s1
long i
CFTimeInterval t
t = fn CACurrentMediaTime... |
Subsets and Splits
Rosetta Code COBOL Python Hard Tasks
Identifies and retrieves challenging tasks that exist in both COBOL and Python, revealing cross-language programming patterns and difficulty levels for comparative analysis.
Rosetta Code Task Comparisons
Identifies tasks common to both COBOL and Python languages that are described as having difficulty levels, revealing cross-language task similarities and providing useful comparative programming examples.
SQL Code Examples from Training Data
Retrieves raw SQL code examples for the SQL language, which is basic data retrieval without meaningful analysis or patterns.
SQL Code Examples from Training Data
Retrieves raw SQL code examples for the SQL language, which is basic filtering that shows what data looks like but doesn't provide meaningful analysis or patterns.
Select Specific Languages Codes
Retrieves specific programming language names and codes from training data, providing basic filtering but limited analytical value beyond identifying these particular languages.