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/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)
... | #Perl | Perl |
use strict;
use warnings;
use feature qw(say);
for my $i (1..100) {
say $i % 15 == 0 ? "FizzBuzz"
: $i % 3 == 0 ? "Fizz"
: $i % 5 == 0 ? "Buzz"
: $i;
} |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Haskell | Haskell | --https://wiki.haskell.org/Power_function
main = do
print [-5^2,-(5)^2,(-5)^2,-(5^2)] --Integer
print [-5^^2,-(5)^^2,(-5)^^2,-(5^^2)] --Fractional
print [-5**2,-(5)**2,(-5)**2,-(5**2)] --Real
print [-5^3,-(5)^3,(-5)^3,-(5^3)] --Integer
print [-5^^3,-(5)^^3,(-5)^^3,-(5^^3)] --Fractional
print [-... |
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 ... | #FALSE | FALSE | [[$0=~][1-@@\$@@+\$44,.@]#]f:
20n: {First 20 numbers}
0 1 n;f;!%%44,. {Output: "0,1,1,2,3,5..."} |
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 |... | #Oberon-2 | Oberon-2 |
MODULE Factors;
IMPORT Out,SYSTEM;
TYPE
LIPool = POINTER TO ARRAY OF LONGINT;
LIVector= POINTER TO LIVectorDesc;
LIVectorDesc = RECORD
cap: INTEGER;
len: INTEGER;
LIPool: LIPool;
END;
PROCEDURE New(cap: INTEGER): LIVector;
VAR
v: LIVector;
BEGIN
NEW(v);
v.cap := cap;
v.len := 0;
NEW(v.LIPool... |
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 ... | #AWK | AWK | BEGIN {
# This requires 1e400 to overflow to infinity.
nzero = -0
nan = 0 * 1e400
pinf = 1e400
ninf = -1e400
print "nzero =", nzero
print "nan =", nan
print "pinf =", pinf
print "ninf =", ninf
print
# When y == 0, sign of x decides if atan2(y, x) is 0 or pi.
print "atan2(0, 0) =", atan2(0, 0)
print "a... |
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.... | #F.23 | F# |
// Factorial base numbers indexing permutations of a collection
// Nigel Galloway: December 7th., 2018
let lN2p (c:int[]) (Ω:'Ω[])=
let Ω=Array.copy Ω
let rec fN i g e l=match l-i with 0->Ω.[i]<-e |_->Ω.[l]<-Ω.[l-1]; fN i g e (l-1)// rotate right
[0..((Array.length Ω)-2)]|>List.iter(fun n->let i=c.[n] in if i>0... |
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.... | #Factor | Factor | USING: assocs io kernel literals math math.factorials
math.parser math.ranges prettyprint qw random sequences
splitting ;
RENAME: factoradic math.combinatorics.private => _factoradic
RENAME: rotate sequences.extras => _rotate
IN: rosetta-code.factorial-permutations
CONSTANT: shoe $[
qw{ A K Q J 10 9 8 7 6 5 4 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... | #360_Assembly | 360 Assembly | * Factorions 26/04/2020
FACTORIO CSECT
USING FACTORIO,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
SAVE (14,12) save previous context
ST R13,4(R15) link backward
ST... |
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... | #ALGOL_68 | ALGOL 68 | BEGIN
# cache factorials from 0 to 11 #
[ 0 : 11 ]INT fact;
fact[0] := 1;
FOR n TO 11 DO
fact[n] := fact[n-1] * n
OD;
FOR b FROM 9 TO 12 DO
print( ( "The factorions for base ", whole( b, 0 ), " are:", newline ) );
FOR i TO 1500000 - 1 DO
INT sum := 0;
... |
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.
| #Seed7 | Seed7 | var array integer: arr is [] (1, 2, 3, 4, 5);
var array integer: evens is 0 times 0;
var integer: number is 0;
for number range arr do
if not odd(number) then
evens &:= [] (number);
end if;
end for; |
http://rosettacode.org/wiki/Faces_from_a_mesh | Faces from a mesh | A mesh defining a surface has uniquely numbered vertices, and named,
simple-polygonal faces described usually by an ordered list of edge numbers
going around the face,
For example:
External image of two faces
Rough textual version without edges:
1
17
7 A
B
... | #Python | Python | def perim_equal(p1, p2):
# Cheap tests first
if len(p1) != len(p2) or set(p1) != set(p2):
return False
if any(p2 == (p1[n:] + p1[:n]) for n in range(len(p1))):
return True
p2 = p2[::-1] # not inplace
return any(p2 == (p1[n:] + p1[:n]) for n in range(len(p1)))
def edge_to_periphery(... |
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)
... | #Phix | Phix | constant x = {"%d\n","Fizz\n","Buzz\n","FizzBuzz\n"}
for i=1 to 100 do
printf(1,x[1+(remainder(i,3)=0)+2*(remainder(i,5)=0)],i)
end for
|
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #J | J | format=: ''&$: :([: ; (a: , [: ":&.> [) ,. '{}' ([ (E. <@}.;._1 ]) ,) ])
S=: 'x = {} p = {} -x^p {} -(x)^p {} (-x)^p {} -(x^p) {}'
explicit=: dyad def 'S format~ 2 2 4 4 4 4 ":&> x,y,(-x^y),(-(x)^y),((-x)^y),(-(x^y))'
tacit=: S format~ 2 2 4 4 4 4 ":&> [`]`([:-^)`([:-^)`((... |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Julia | Julia |
for x in [-5, 5], p in [2, 3]
println("x is", lpad(x, 3), ", p is $p, -x^p is", lpad(-x^p, 4), ", -(x)^p is",
lpad(-(x)^p, 5), ", (-x)^p is", lpad((-x)^p, 5), ", -(x^p) is", lpad(-(x^p), 5))
end
|
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Lua | Lua | mathtype = math.type or type -- polyfill <5.3
function test(xs, ps)
for _,x in ipairs(xs) do
for _,p in ipairs(ps) do
print(string.format("%2.f %7s %2.f %7s %4.f %4.f %4.f %4.f", x, mathtype(x), p, mathtype(p), -x^p, -(x)^p, (-x)^p, -(x^p)))
end
end
end
print(" x type(x) p type(p) ... |
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 ... | #Fancy | Fancy | class Fixnum {
def fib {
match self -> {
case 0 -> 0
case 1 -> 1
case _ -> self - 1 fib + (self - 2 fib)
}
}
}
15 times: |x| {
x fib println
}
|
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 |... | #Objeck | Objeck | use IO;
use Structure;
bundle Default {
class Basic {
function : native : GenerateFactors(n : Int) ~ IntVector {
factors := IntVector->New();
factors-> AddBack(1);
factors->AddBack(n);
for(i := 2; i * i <= n; i += 1;) {
if(n % i = 0) {
factors->AddBack(i);
... |
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 ... | #bc | bc | $ bc
# trying for negative zero
-0
0
# trying to underflow to negative zero
-1 / 2
0
# trying for NaN (not a number)
0 / 0
dc: divide by zero
0
sqrt(-1)
dc: square root of negative number
dc: stack empty
dc: stack empty
|
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 ... | #C | C | #include <stdio.h>
int main()
{
double inf = 1/0.0;
double minus_inf = -1/0.0;
double minus_zero = -1/ inf ;
double nan = 0.0/0.0;
printf("positive infinity: %f\n",inf);
printf("negative infinity: %f\n",minus_inf);
printf("negative zero: %f\n",minus_zero);
printf("not a number: %f\n"... |
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.... | #Go | Go | package main
import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
func factorial(n int) int {
fact := 1
for i := 2; i <= n; i++ {
fact *= i
}
return fact
}
func genFactBaseNums(size int, countOnly bool) ([][]int, int) {
var results [][]int
count := 0
for ... |
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... | #Applesoft_BASIC | Applesoft BASIC | 100 DIM FACT(12)
110 FACT(0) = 1
120 FOR N = 1 TO 11
130 FACT(N) = FACT(N - 1) * N
140 NEXT
200 FOR B = 9 TO 12
210 PRINT "THE FACTORIONS ";
215 PRINT "FOR BASE "B" ARE:"
220 FOR I = 1 TO 1499999
230 SUM = 0
240 FOR J = I TO 0 STEP 0
245 M = INT (J / B)
250 D = ... |
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... | #Arturo | Arturo | factorials: [1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800]
factorion?: function [n, base][
try? [
n = sum map digits.base:base n 'x -> factorials\[x]
]
else [
print ["n:" n "base:" base]
false
]
]
loop 9..12 'base ->
print ["Base" base "factorions:" select 1..450... |
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.
| #SequenceL | SequenceL | evens(x(1))[i] := x[i] when x[i] mod 2 = 0; |
http://rosettacode.org/wiki/Faces_from_a_mesh | Faces from a mesh | A mesh defining a surface has uniquely numbered vertices, and named,
simple-polygonal faces described usually by an ordered list of edge numbers
going around the face,
For example:
External image of two faces
Rough textual version without edges:
1
17
7 A
B
... | #Raku | Raku | sub check-equivalence ($a, $b) { so $a.Bag eqv $b.Bag }
sub edge-to-periphery (@a is copy) {
return Nil unless @a.List.Bag.values.all == 2;
my @b = @a.shift.flat;
while @a > 1 {
for @a.kv -> $k, $v {
if $v[0] == @b.tail {
@b.push: $v[1];
@a.splice($k,1);... |
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)
... | #PHL | PHL | module fizzbuzz;
extern printf;
@Integer main [
var i = 1;
while (i <= 100) {
if (i % 15 == 0)
printf("FizzBuzz");
else if (i % 3 == 0)
printf("Fizz");
else if (i % 5 == 0)
printf("Buzz");
else
printf("%d", i);
printf("\n");
i = i::inc;
}
return 0;
] |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Maple | Maple | [-5**2,-(5)**2,(-5)**2,-(5**2)];
[-25, -25, 25, -25]
[-5**3,-(5)**3,(-5)**3,-(5**3)];
[-125, -125, -125, -125] |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | {-5^2, -(5)^2, (-5)^2, -(5^2)}
{-5^3, -(5)^3, (-5)^3, -(5^3)} |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Nim | Nim | import math, strformat
for x in [-5, 5]:
for p in [2, 3]:
echo &"x is {x:2}, ", &"p is {p:1}, ",
&"-x^p is {-x^p:4}, ", &"-(x)^p is {-(x)^p:4}, ",
&"(-x)^p is {(-x)^p:4}, ", &"-(x^p) is {-(x^p):4}" |
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 ... | #Fantom | Fantom |
class Main
{
static Int fib (Int n)
{
if (n < 2) return n
fibNums := [1, 0]
while (fibNums.size <= n)
{
fibNums.insert (0, fibNums[0] + fibNums[1])
}
return fibNums.first
}
public static Void main ()
{
20.times |n|
{
echo ("Fib($n) is ${fib(n)}")
}
}
}
|
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 |... | #OCaml | OCaml | let rec range = function 0 -> [] | n -> range(n-1) @ [n]
let factors n =
List.filter (fun v -> (n mod v) = 0) (range 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 ... | #Clojure | Clojure |
(def neg-inf (/ -1.0 0.0)) ; Also Double/NEGATIVE_INFINITY
(def inf (/ 1.0 0.0)) ; Also Double/POSITIVE_INFINITY
(def nan (/ 0.0 0.0)) ; Also Double/NaN
(def neg-zero (/ -2.0 Double/POSITIVE_INFINITY)) ; Also -0.0
(println " Negative inf: " neg-inf)
(println " Positive inf: " inf)
(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 ... | #D | D | // Compile this module without -O
import std.stdio: writeln, writefln;
import std.string: format;
import std.math: NaN, getNaNPayload;
void show(T)() {
static string toHex(T x) {
string result;
auto ptr = cast(ubyte*)&x;
foreach_reverse (immutable i; 0 .. T.sizeof)
result ~= ... |
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.... | #Haskell | Haskell | import Data.List (unfoldr, intercalate)
newtype Fact = Fact [Int]
-- smart constructor
fact :: [Int] -> Fact
fact = Fact . zipWith min [0..] . reverse
instance Show Fact where
show (Fact ds) = intercalate "." $ show <$> reverse ds
toFact :: Integer -> Fact
toFact 0 = Fact [0]
toFact n = Fact $ unfoldr f (1, n... |
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.... | #J | J | NB. A is a numerical matrix corresponding to the input and output
A =: _&".;._2[0 :0
0 0 0 0123
0 0 1 0132
0 1 0 0213
0 1 1 0231
0 2 0 0312
0 2 1 0321
1 0 0 1023
1 0 1 1032
1 1 0 1203
1 1 1 1230
1 2 0 1302
1 2 1 1320
2 0 0 ... |
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... | #AutoHotkey | AutoHotkey | fact:=[]
fact[0] := 1
while (A_Index < 12)
fact[A_Index] := fact[A_Index-1] * A_Index
b := 9
while (b <= 12) {
res .= "base " b " factorions: `t"
while (A_Index < 1500000){
sum := 0
j := A_Index
while (j > 0){
d := Mod(j, b)
sum += fact[d]
j /= b
}
if (sum = A_Index)
res .= A_Index " "
}
b... |
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... | #AWK | AWK |
# syntax: GAWK -f FACTORIONS.AWK
# converted from C
BEGIN {
fact[0] = 1 # cache factorials from 0 to 11
for (n=1; n<12; ++n) {
fact[n] = fact[n-1] * n
}
for (b=9; b<=12; ++b) {
printf("base %d factorions:",b)
for (i=1; i<1500000; ++i) {
sum = 0
j = i
while (j ... |
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.
| #Sidef | Sidef | var arr = [1,2,3,4,5];
# Creates a new array
var new = arr.grep {|i| i %% 2};
say new.dump; # => [2, 4]
# Destructive (at variable level)
arr.grep! {|i| i %% 2};
say arr.dump; # => [2, 4] |
http://rosettacode.org/wiki/Faces_from_a_mesh | Faces from a mesh | A mesh defining a surface has uniquely numbered vertices, and named,
simple-polygonal faces described usually by an ordered list of edge numbers
going around the face,
For example:
External image of two faces
Rough textual version without edges:
1
17
7 A
B
... | #Wren | Wren | import "./sort" for Sort
import "./seq" for Lst
import "./fmt" for Fmt
// Check two perimeters are equal.
var perimEqual = Fn.new { |p1, p2|
var le = p1.count
if (le != p2.count) return false
for (p in p1) {
if (!p2.contains(p)) return false
}
// use copy to avoid mutating 'p1'
var c =... |
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)
... | #PHP | PHP | <?php
for ($i = 1; $i <= 100; $i++)
{
if (!($i % 15))
echo "FizzBuzz\n";
else if (!($i % 3))
echo "Fizz\n";
else if (!($i % 5))
echo "Buzz\n";
else
echo "$i\n";
}
?> |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Pascal | Pascal | program exponentiationWithInfixOperatorsInTheBase(output);
const
minimumWidth = 7;
fractionDigits = minimumWidth div 4 + 1;
procedure testIntegerPower(
{ `pow` can in fact accept `integer`, `real` and `complex`. }
protected base: integer;
{ For `pow` the `exponent` _has_ to be an `integer`. }
protected ex... |
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 ... | #Fermat | Fermat | Func Fibonacci(n) = if n<0 then -(-1)^n*Fibonacci(-n) else if n<2 then n else
Array fib[n+1];
fib[1] := 0;
fib[2] := 1;
for i = 2, n do
fib[i+1]:=fib[i]+fib[i-1]
od;
Return(fib[n+1]);
fi;
fi;
. |
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 |... | #Oforth | Oforth | Integer method: factors self seq filter(#[ self isMultiple ]) ;
120 factors 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 ... | #Delphi | Delphi | program Floats;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
PlusInf, MinusInf, NegZero, NotANum: Double;
begin
PlusInf:= 1.0/0.0;
MinusInf:= -1.0/0.0;
NegZero:= -1.0/PlusInf;
NotANum:= 0.0/0.0;
Writeln('Positive Infinity: ', PlusInf); // +Inf
Writeln('Negative Infinity: ', MinusInf); // -In... |
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.... | #Julia | Julia | function makefactorialbased(N, makelist)
listlist = Vector{Vector{Int}}()
count = 0
while true
divisor = 2
makelist && (lis = zeros(Int, N))
k = count
while k > 0
k, r = divrem(k, divisor)
makelist && (divisor <= N + 1) && (lis[N - divisor + 2] = r)
... |
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.... | #Nim | Nim | import algorithm, math, random, sequtils, strutils, unicode
# Representation of a factorial base number with N digits.
type FactorialBaseNumber[N: static Positive] = array[N, int]
#---------------------------------------------------------------------------------------------------
func permutation[T](elements: ope... |
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... | #C | C | #include <stdio.h>
int main() {
int n, b, d;
unsigned long long i, j, sum, fact[12];
// cache factorials from 0 to 11
fact[0] = 1;
for (n = 1; n < 12; ++n) {
fact[n] = fact[n-1] * n;
}
for (b = 9; b <= 12; ++b) {
printf("The factorions for base %d are:\n", b);
... |
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... | #C.2B.2B | C++ | #include <iostream>
class factorion_t {
public:
factorion_t() {
f[0] = 1u;
for (uint n = 1u; n < 12u; n++)
f[n] = f[n - 1] * n;
}
bool operator()(uint i, uint b) const {
uint sum = 0;
for (uint j = i; j > 0u; j /= b)
sum += f[j % b];
return... |
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.
| #Slate | Slate | #(1 2 3 4 5) select: [| :number | number isEven]. |
http://rosettacode.org/wiki/Faces_from_a_mesh | Faces from a mesh | A mesh defining a surface has uniquely numbered vertices, and named,
simple-polygonal faces described usually by an ordered list of edge numbers
going around the face,
For example:
External image of two faces
Rough textual version without edges:
1
17
7 A
B
... | #zkl | zkl | fcn perimSame(p1, p2){
if(p1.len() != p2.len()) return(False);
False == p1.filter1('wrap(p){ (not p2.holds(p)) })
}
fcn edge_to_periphery(faces){
edges:=faces.copy().sort(fcn(a,b){ if(a[0]!=b[0]) a[0]<b[0] else a[1]<b[1] });
p,last := ( if(edges) edges.pop(0).copy() else T ), ( p and p[-1] or Void );
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)
... | #Picat | Picat | fizzbuzz_map =>
println(fizzbuzz_map),
FB = [I : I in 1..100],
Map = [(3="Fizz"),(5="Buzz"),(15="FizzBuzz")],
foreach(I in FB, K=V in Map)
if I mod K == 0 then
FB[I] := V
end
end,
println(FB). |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Perl | Perl | use strict;
use warnings;
use Sub::Infix;
BEGIN { *e = infix { $_[0] ** $_[1] } }; # operator needs to be defined at compile time
my @eqns = ('1 + -$xOP$p', '1 + (-$x)OP$p', '1 + (-($x)OP$p)', '(1 + -$x)OP$p', '1 + -($xOP$p)');
for my $op ('**', '/e/', '|e|') {
for ( [-5, 2], [-5, 3], [5, 2], [5, 3] ) {
... |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Phix | Phix | for x=-5 to 5 by 10 do
for p=2 to 3 do
printf(1,"x = %2d, p = %d, power(-x,p) = %4d, -power(x,p) = %4d\n",{x,p,power(-x,p),-power(x,p)})
end for
end for
|
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 ... | #Fexl | Fexl |
# (fib n) = the nth Fibonacci number
\fib=
(
\loop==
(\x\y\n
le n 0 x;
\z=(+ x y)
\n=(- n 1)
loop y z n
)
loop 0 1
)
# Now test it:
for 0 20 (\n say (fib n))
|
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 |... | #Oz | Oz | declare
fun {Factors N}
Sqr = {Float.toInt {Sqrt {Int.toFloat N}}}
Fs = for X in 1..Sqr append:App do
if N mod X == 0 then
CoFactor = N div X
in
if CoFactor == X then %% avoid duplicate factor
{App [X]} %% when N is a sq... |
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 ... | #Eiffel | Eiffel |
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
-- Run application.
local
negInf, posInf, negZero, nan: REAL_64
do
negInf := -1. / 0. -- also {REAL_64}.negative_infinity
posInf := 1. / 0. -- also {REAL_64}.positive_infinity
negZero := -1. / posInf
na... |
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.... | #Perl | Perl | use strict;
use warnings;
use feature 'say';
sub fpermute {
my($f,@a) = @_;
my @f = split /\./, $f;
for (0..$#f) {
my @b = @a[$_ .. $_+$f[$_]];
unshift @b, splice @b, $#b, 1; # rotate(-1)
@a[$_ .. $_+$f[$_]] = @b;
}
join '', @a;
}
sub base {
my($n) = @_;
my @digi... |
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... | #Common_Lisp | Common Lisp | (defparameter *bases* '(9 10 11 12))
(defparameter *limit* 1500000)
(defun ! (n) (apply #'* (loop for i from 2 to n collect i)))
(defparameter *digit-factorials* (mapcar #'! (loop for i from 0 to (1- (apply #'max *bases*)) collect i)))
(defun fact (n) (nth n *digit-factorials*))
(defun digit-value (digit)
(le... |
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.
| #Smalltalk | Smalltalk | #(1 2 3 4 5) select: [:number | number 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)
... | #PicoLisp | PicoLisp | (for N 100
(prinl
(or (pack (at (0 . 3) "Fizz") (at (0 . 5) "Buzz")) N) ) )
# Above, we simply count till 100 'prin'-ting number 'at' 3rd ('Fizz'), 5th ('Buzz') and 'pack'-ing 15th number ('FizzBuzz').
# Rest of the times 'N' is printed as it loops in 'for'.
|
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #QB64 | QB64 | For x = -5 To 5 Step 10
For p = 2 To 3
Print "x = "; x; " p ="; p; " ";
Print Using "-x^p is ####"; -x ^ p;
Print Using ", -(x)^p is ####"; -(x) ^ p;
Print Using ", (-x)^p is ####"; (-x) ^ p;
Print Using ", -(x^p) is ####"; -(x ^ p)
Next
Next |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #R | R | expressions <- alist(-x ^ p, -(x) ^ p, (-x) ^ p, -(x ^ p))
x <- c(-5, -5, 5, 5)
p <- c(2, 3, 2, 3)
output <- data.frame(x,
p,
setNames(lapply(expressions, eval), sapply(expressions, deparse)),
check.names = FALSE)
print(output, row.names = FALSE) |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Raku | Raku | sub infix:<↑> is looser(&prefix:<->) { $^a ** $^b }
sub infix:<^> is looser(&infix:<->) { $^a ** $^b }
use MONKEY;
for 'Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.',
('1 + -$x**$p', '1 + (-$x)**$p', '1 + (-($x)**$p)', '(1 + -$x)**$p', '1 + -($x**$p)'),
"\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 ... | #Fish | Fish | 10::n' 'o&+&$10. |
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 |... | #Panda | Panda | fun factor(n) type integer->integer
f where n.mod(1..n=>f)==0
45.factor |
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 ... | #Euphoria | Euphoria | constant inf = 1E400
constant minus_inf = -inf
constant nan = 0*inf
printf(1,"positive infinity: %f\n", inf)
printf(1,"negative infinity: %f\n", minus_inf)
printf(1,"not a number: %f\n", nan)
-- some arithmetic
printf(1,"+inf + 2.0 = %f\n", inf + 2.0)
printf(1,"+inf - 10.1 = %f\n", inf - 10.1)
printf(1,"+inf + -i... |
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 ... | #F.23 | F# |
0.0/0.0 //->nan
0.0/(-0.0) //->nan
1.0/infinity //->0.0
1.0/(-infinity) //->0.0
1.0/0.0 //->infinity
1.0/(-0.0) //->-infinity
-infinity<infinity //->true
(-0.0)<0.0 //->false
|
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.... | #Phix | Phix | with javascript_semantics
function fperm(sequence fbn, omega)
integer m=0
for i=1 to length(fbn) do
integer g = fbn[i]
if g>0 then
omega[m+1..m+g+1] = omega[m+g+1]&omega[m+1..m+g]
end if
m += 1
end for
return omega
end function
function factorial_base_number... |
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... | #Delphi | Delphi |
program Factorions;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
begin
var fact: TArray<UInt64>;
SetLength(fact, 12);
fact[0] := 0;
for var n := 1 to 11 do
fact[n] := fact[n - 1] * n;
for var b := 9 to 12 do
begin
writeln('The factorions for base ', b, ' are:');
for var i := 1 to 14999... |
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... | #F.23 | F# |
// Factorians. Nigel Galloway: October 22nd., 2021
let N=[|let mutable n=1 in yield n; for g in 1..11 do n<-n*g; yield n|]
let fG n g=let rec fN g=function i when i<n->g+N.[i] |i->fN(g+N.[i%n])(i/n) in fN 0 g
{9..12}|>Seq.iter(fun n->printf $"In base %d{n} Factorians are:"; {1..1500000}|>Seq.iter(fun g->if g=fG n g... |
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.
| #SQL | SQL | --Create the original array (table #nos) with numbers from 1 to 10
CREATE TABLE #nos (v INT)
DECLARE @n INT SET @n=1
while @n<=10 BEGIN INSERT INTO #nos VALUES (@n) SET @n=@n+1 END
--Select the subset that are even into the new array (table #evens)
SELECT v INTO #evens FROM #nos WHERE v % 2 = 0
-- Show #evens
SELEC... |
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)
... | #Pike | Pike | int main(){
for(int i = 1; i <= 100; i++) {
if(i % 15 == 0) {
write("FizzBuzz\n");
} else if(i % 3 == 0) {
write("Fizz\n");
} else if(i % 5 == 0) {
write("Buzz\n");
} else {
write(i + "\n");
}
}
} |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #REXX | REXX | /*REXX program shows exponentition with an infix operator (implied and/or specified).*/
_= '─'; ! = '║'; mJunct= '─╫─'; bJunct= '─╨─' /*define some special glyphs. */
say @(' x ', 5) @(" p ", 5) !
say @('value', 5) @("value", 5) copies(! @('expression',10) @("result",6)" ", 4)
say ... |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Ruby | Ruby | nums = [-5, 5]
pows = [2, 3]
nums.product(pows) do |x, p|
puts "x = #{x} p = #{p}\t-x**p #{-x**p}\t-(x)**p #{-(x)**p}\t(-x)**p #{ (-x)**p}\t-(x**p) #{-(x**p)}"
end
|
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 ... | #FOCAL | FOCAL | 01.10 TYPE "FIBONACCI NUMBERS" !
01.20 ASK "N =", N
01.30 SET A=0
01.40 SET B=1
01.50 FOR I=2,N; DO 2.0
01.60 TYPE "F(N) ", %8, B, !
01.70 QUIT
02.10 SET T=B
02.20 SET B=A+B
02.30 SET A=T |
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 |... | #PARI.2FGP | PARI/GP | divisors(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 ... | #Factor | Factor | -0. . ! -0.0 literal negative zero
0. neg . ! -0.0 neg works with floating point zeros
0. -1. * . ! -0.0 calculating negative zero
1/0. . ! 1/0. literal positive infinity
1e3 1e3 ^ . ! 1/0. calculating positive infinity
-1/0. . ! -1/0. literal negative infinit... |
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 ... | #Forth | Forth | 1e 0e f/ f. \ inf
-1e 0e f/ f. \ inf (output bug: should say "-inf")
-1e 0e f/ f0< . \ -1 (true, it is -inf)
0e 0e f/ f. \ nan
-1e 0e f/ 1/f f0< . \ 0 (false, can't represent IEEE negative 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... | #ABAP | ABAP | DATA(result) = COND #( WHEN condition1istrue = abap_true AND condition2istrue = abap_true THEN bothconditionsaretrue
WHEN condition1istrue = abap_true THEN firstconditionistrue
WHEN condition2istrue = abap_true THEN secondconditionistrue
ELSE... |
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.... | #Python | Python |
"""
http://rosettacode.org/wiki/Factorial_base_numbers_indexing_permutations_of_a_collection
https://en.wikipedia.org/wiki/Factorial_number_system
"""
import math
def apply_perm(omega,fbn):
"""
omega contains a list which will be permuted (scrambled)
based on fbm.
fbm is a list which rep... |
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... | #Factor | Factor | USING: formatting io kernel math math.parser math.ranges memoize
prettyprint sequences ;
IN: rosetta-code.factorions
! Memoize factorial function
MEMO: factorial ( n -- n! ) [ 1 ] [ [1,b] product ] if-zero ;
: factorion? ( n base -- ? )
dupd >base string>digits [ factorial ] map-sum = ;
: show-factorions ( li... |
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... | #F.C5.8Drmul.C3.A6 | Fōrmulæ | Dim As Integer fact(12), suma, d, j
fact(0) = 1
For n As Integer = 1 To 11
fact(n) = fact(n-1) * n
Next n
For b As Integer = 9 To 12
Print "Los factoriones para base " & b & " son: "
For i As Integer = 1 To 1499999
suma = 0
j = i
While j > 0
d = j Mod b
suma +... |
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.
| #Stata | Stata | mata
a=2,9,4,7,5,3,6,1,8
// Select even elements of a
select(a,mod(a,2):==0)
// Select the indices of even elements of a
selectindex(mod(a,2):==0)
end |
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)
... | #PILOT | PILOT | C :i = 0
*loop
C :i = i + 1
J ( i > 100 ) : *finished
C :modulo = i % 15
J ( modulo = 0 ) : *fizzbuzz
C :modulo = i % 3
J ( modulo = 0 ) : *fizz
C :modulo = i % 5
J ( modulo = 0 ) : *buzz
T :#i
J : *loop
*fizzbuzz
T :FizzBuzz
J : *loop
*fizz
T :Fizz
J : *loop
*buzz
T :Buzz
J : *loop
*finished
END: |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Python | Python | from itertools import product
xx = '-5 +5'.split()
pp = '2 3'.split()
texts = '-x**p -(x)**p (-x)**p -(x**p)'.split()
print('Integer variable exponentiation')
for x, p in product(xx, pp):
print(f' x,p = {x:2},{p}; ', end=' ')
x, p = int(x), int(p)
print('; '.join(f"{t} =={eval(t):4}" for t in texts))
... |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Smalltalk | Smalltalk | a := 2.
b := 3.
Transcript show:'-5**2 => '; showCR: -5**2.
Transcript show:'-5**a => '; showCR: -5**a.
Transcript show:'-5**b => '; showCR: -5**b.
Transcript show:'5**2 => '; showCR: 5**2.
Transcript show:'5**a => '; showCR: 5**a.
Transcript show:'5**b => '; showCR: 5**b.
" Transcript show:'-(5**b) => '; showCR: -(5**... |
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 ... | #Forth | Forth | : fib ( n -- fib )
0 1 rot 0 ?do over + swap loop drop ; |
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 |... | #Pascal | Pascal | program Factors;
var
i, number: integer;
begin
write('Enter a number between 1 and 2147483647: ');
readln(number);
for i := 1 to round(sqrt(number)) - 1 do
if number mod i = 0 then
write (i, ' ', number div i, ' ');
// Check to see if number is a square
i := round(sqrt(number));
if i*i = 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 ... | #Fortran | Fortran |
REAL*8 BAD,NaN !Sometimes a number is not what is appropriate.
PARAMETER (NaN = Z'FFFFFFFFFFFFFFFF') !This value is recognised in floating-point arithmetic.
PARAMETER (BAD = Z'FFFFFFFFFFFFFFFF') !I pay special attention to BAD values.
CHARACTER*3 BADASTEXT !Speakable form.
DATA ... |
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 ... | #FreeBASIC | FreeBASIC | ' FB 1.05.0 Win64
#Include "crt/math.bi"
Dim inf As Double = INFINITY
Dim negInf As Double = -INFINITY
Dim notNum As Double = NAN_
Dim negZero As Double = 1.0 / negInf
Print inf, inf / inf
Print negInf, negInf * negInf
Print notNum, notNum + inf + negInf
Print negZero, negZero - 1
Sleep |
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... | #6502_Assembly | 6502 Assembly | CMP_Double .macro ;NESASM3 syntax
; input:
; \1 = first condition. Valid addressing modes: immediate, zero page, or absolute.
; \2 = second condition. Valid addressing modes: immediate, zero page, or absolute.
; \3 = branch here if both are true
; \4 = branch here if only first condition is true
; \5 = branch here i... |
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... | #Ada | Ada | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_If_2 is
type Two_Bool is range 0 .. 3;
function If_2(Cond_1, Cond_2: Boolean) return Two_Bool is
(Two_Bool(2*Boolean'Pos(Cond_1)) + Two_Bool(Boolean'Pos(Cond_2)));
begin
for N in 10 .. 20 loop
Put(Integer'Image(N) & " is ");
case If_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.... | #Quackery | Quackery | [ 1 swap times [ i 1+ * ] ] is ! ( n --> n )
[ dup 0 = iff [ drop 2 ] done
0
[ 1+ 2dup ! / 0 = until ]
nip ] is figits ( n --> n )
[ [] unrot 1 - times
[ i 1+ ! /mod
dip join ] drop ] is factoradic ( n n --> [ )
[ 0 swap
... |
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.... | #Raku | Raku | sub postfix:<!> (Int $n) { (flat 1, [\*] 1..*)[$n] }
multi base (Int $n is copy, 'F', $length? is copy) {
constant @fact = [\*] 1 .. *;
my $i = $length // @fact.first: * > $n, :k;
my $f;
[ @fact[^$i].reverse.map: { ($n, $f) = $n.polymod($_); $f } ]
}
sub fpermute (@a is copy, *@f) { (^@f).map: { @a[... |
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... | #FreeBASIC | FreeBASIC | Dim As Integer fact(12), suma, d, j
fact(0) = 1
For n As Integer = 1 To 11
fact(n) = fact(n-1) * n
Next n
For b As Integer = 9 To 12
Print "Los factoriones para base " & b & " son: "
For i As Integer = 1 To 1499999
suma = 0
j = i
While j > 0
d = j Mod b
suma +... |
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... | #Frink | Frink | factorion[n, base] := sum[map["factorial", integerDigits[n, base]]]
for base = 9 to 12
{
for n = 1 to 1_499_999
if n == factorion[n, base]
println["$base\t$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.
| #Swift | Swift | let numbers = [1,2,3,4,5,6]
let even_numbers = numbers.filter { $0 % 2 == 0 }
println(even_numbers) |
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)
... | #PIR | PIR | .sub main :main
.local int f
.local int mf
.local int skipnum
f = 1
LOOP:
if f > 100 goto DONE
skipnum = 0
mf = f % 3
if mf == 0 goto FIZZ
FIZZRET:
mf = f % 5
if mf == 0 goto BUZZ
BUZZRET:
if skipnum > 0 goto SKIPNUM
print f
SKIPNUM:
print "\n"
inc f
goto LOOP
end
FIZZ:
print "Fizz"
... |
http://rosettacode.org/wiki/Exponentiation_with_infix_operators_in_(or_operating_on)_the_base | Exponentiation with infix operators in (or operating on) the base | (Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **, ^, ↑ or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if ... | #Wren | Wren | import "/fmt" for Fmt
class Num2 {
construct new(n) { _n = n }
n { _n}
^(exp) {
if (exp is Num2) exp = exp.n
return Num2.new(_n.pow(exp))
}
- { Num2.new(-_n) }
toString { _n.toString }
}
var ops = ["-x^p", "-(x)^p", "(-x)^p", "-(x^p)"]
for (x in [Num2.new(-5), Num2.... |
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... | #Ada | Ada | with Ada.Text_IO, Miller_Rabin;
procedure Prime_Gen is
type Num is range 0 .. 2**63-1; -- maximum for the gnat Ada compiler
MR_Iterations: constant Positive := 25;
-- the probability Pr[Is_Prime(N, MR_Iterations) = Probably_Prime]
-- is 1 for prime N and < 4**(-MR_Iterations) for composed 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 ... | #Fortran | Fortran | C FIBONACCI SEQUENCE - FORTRAN IV
NN=46
DO 1 I=0,NN
1 WRITE(*,300) I,IFIBO(I)
300 FORMAT(1X,I2,1X,I10)
END
C
FUNCTION IFIBO(N)
IF(N) 9,1,2
1 IFN=0
GOTO 9
2 IF(N-1) 9,3,4
3 IFN=1
GOTO 9
4 IFNM1=0
IFN=1
DO 5 I=2,N
IFNM2=IFNM1
IFNM... |
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.