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/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #D | D | import core.stdc.stdio, std.math, std.typecons, std.string, std.conv,
std.algorithm, std.ascii, std.array, bitmap, grayscale_image;
enum maxBrightness = 255;
alias Pixel = short;
alias IntT = typeof(size_t.init.signed);
// If normalize is true, map pixels to range 0...maxBrightness.
void convolution(bool n... |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #C.2B.2B | C++ | #include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>
// Class representing an IPv4 address + netmask length
class ipv4_cidr {
public:
ipv4_cidr() {}
ipv4_cidr(std::uint32_t address, unsigned int mask_length)
: address_(address), mask_length_(mask_length) {}
std::uint32_t ad... |
http://rosettacode.org/wiki/Casting_out_nines | Casting out nines | Task (in three parts)
Part 1
Write a procedure (say
c
o
9
(
x
)
{\displaystyle {\mathit {co9}}(x)}
) which implements Casting Out Nines as described by returning the checksum for
x
{\displaystyle x}
. Demonstrate the procedure using the examples given there, or others you may consider lucky.
Par... | #D | D | import std.stdio, std.algorithm, std.range;
uint[] castOut(in uint base=10, in uint start=1, in uint end=999999) {
auto ran = iota(base - 1)
.filter!(x => x % (base - 1) == (x * x) % (base - 1));
auto x = start / (base - 1);
immutable y = start % (base - 1);
typeof(return) result;
... |
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface | Catmull–Clark subdivision surface | Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals.
The proce... | #Nim | Nim | import algorithm
import tables
const None = -1 # Index number used to indicate no data.
type
Point = array[3, float]
Face = seq[int]
Edge = object
pn1: int # Point number 1.
pn2: int # Point number 2.
fn1: int # Face number 1.
fn2: int # Face number 2.
cp: Point # Cente... |
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes | Carmichael 3 strong pseudoprimes | A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it.
The Miller Rabin Test uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this.
The purpose of this task is to investigate such numbers using a met... | #C.2B.2B | C++ | #include <iomanip>
#include <iostream>
int mod(int n, int d) {
return (d + n % d) % d;
}
bool is_prime(int n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
for (int p = 5; p * p <= n; p += 4) {
if (n % p == 0)
... |
http://rosettacode.org/wiki/Catamorphism | Catamorphism | Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Task
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
See also
Wikipedia article: ... | #CLU | CLU | % Reduction.
% First type = sequence type (must support S$elements and yield R)
% Second type = right (input) datatype
% Third type = left (output) datatype
reduce = proc [S,R,L: type] (f: proctype (L,R) returns (L),
id: L,
seq: S)
returns (L)
... |
http://rosettacode.org/wiki/Catamorphism | Catamorphism | Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Task
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
See also
Wikipedia article: ... | #Common_Lisp | Common Lisp | ; Basic usage
> (reduce #'* '(1 2 3 4 5))
120
; Using an initial value
> (reduce #'+ '(1 2 3 4 5) :initial-value 100)
115
; Using only a subsequence
> (reduce #'+ '(1 2 3 4 5) :start 1 :end 4)
9
; Apply a function to each element first
> (reduce #'+ '((a 1) (b 2) (c 3)) :key #'cadr)
6
; Right-associative reduction
> (r... |
http://rosettacode.org/wiki/Chaocipher | Chaocipher | Description
The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010.
The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here.
... | #Phix | Phix | -- demo\rosetta\Chao_cipher.exw
with javascript_semantics
constant l_alphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ",
r_alphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
enum ENCRYPT, DECRYPT
function chao_cipher(string s, integer mode, bool show_steps)
integer len = length(s)
string out = repeat(' ',len),
... |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #jq | jq | def binomial(n; k):
if k > n / 2 then binomial(n; n-k)
else reduce range(1; k+1) as $i (1; . * (n - $i + 1) / $i)
end;
# Direct (naive) computation using two numbers in Pascal's triangle:
def catalan_by_pascal: . as $n | binomial(2*$n; $n) - binomial(2*$n; $n-1); |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #Julia | Julia | # v0.6
function pascal(n::Int)
r = ones(Int, n, n)
for i in 2:n, j in 2:n
r[i, j] = r[i-1, j] + r[i, j-1]
end
return r
end
function catalan_num(n::Int)
p = pascal(n + 2)
p[n+4:n+3:end-1] - diag(p, 2)
end
@show catalan_num(15) |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #Haskell | Haskell | import Text.Printf
main = printf "The three dogs are named %s, %s and %s.\n" dog dOG dOg
where dog = "Benjamin"
dOG = "Samba"
dOg = "Bernie" |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #Icon_and_Unicon | Icon and Unicon | procedure main()
dog := "Benjamin"
Dog := "Samba"
DOG := "Bernie"
if dog == DOG then
write("There is just one dog named ", dog,".")
else
write("The three dogs are named ", dog, ", ", Dog, " and ", DOG, ".")
end |
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists | Cartesian product of two or more lists | Task
Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language.
Demonstrate that your function/method correctly returns:
{1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)}
and, in contrast:
{3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)}
Also demonstrate, using y... | #D | D | import std.stdio;
void main() {
auto a = listProduct([1,2], [3,4]);
writeln(a);
auto b = listProduct([3,4], [1,2]);
writeln(b);
auto c = listProduct([1,2], []);
writeln(c);
auto d = listProduct([], [1,2]);
writeln(d);
}
auto listProduct(T)(T[] ta, T[] tb) {
struct Result {
... |
http://rosettacode.org/wiki/Catalan_numbers | Catalan numbers | Catalan numbers
You are encouraged to solve this task according to the task description, using any language you may know.
Catalan numbers are a sequence of numbers which can be defined directly:
C
n
=
1
n
+
1
(
2
n
n
)
=
(
2
n
)
!
(
n
+
1
)
!
n
!
for
n
≥
0.
{\displaystyle C... | #AutoHotkey | AutoHotkey | Loop 15
out .= "`n" Catalan(A_Index)
Msgbox % clipboard := SubStr(out, 2)
catalan( n ) {
; By [VxE]. Returns ((2n)! / ((n + 1)! * n!)) if 0 <= N <= 22 (higher than 22 results in overflow)
If ( n < 3 ) ; values less than 3 are handled specially
Return n < 0 ? "" : n = 0 ? 1 : n
i := 1 ; initialize the accumulato... |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #ActionScript | ActionScript | // Static
MyClass.method(someParameter);
// Instance
myInstance.method(someParameter); |
http://rosettacode.org/wiki/Cantor_set | Cantor set | Task
Draw a Cantor set.
See details at this Wikipedia webpage: Cantor set
| #ALGOL_68 | ALGOL 68 | BEGIN
# draw a Cantor Set using ASCII #
INT lines = 5; # number of lines for the set #
# we must choose the line width so that the width of each segment is #
# divisible by 3 ( except for the final line where the segment widt... |
http://rosettacode.org/wiki/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #Go | Go | package main
import (
ed "github.com/Ernyoke/Imger/edgedetection"
"github.com/Ernyoke/Imger/imgio"
"log"
)
func main() {
img, err := imgio.ImreadRGBA("Valve_original_(1).png")
if err != nil {
log.Fatal("Could not read image", err)
}
cny, err := ed.CannyRGBA(img, 15, 45, 5)
... |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #C.23 | C# | using System;
using System.Net;
using System.Linq;
public class Program
{
public static void Main()
{
string[] tests = {
"87.70.141.1/22",
"36.18.154.103/12",
"62.62.197.11/29",
"67.137.119.181/4",
"161.214.74.21/24",
"184.232.176... |
http://rosettacode.org/wiki/Casting_out_nines | Casting out nines | Task (in three parts)
Part 1
Write a procedure (say
c
o
9
(
x
)
{\displaystyle {\mathit {co9}}(x)}
) which implements Casting Out Nines as described by returning the checksum for
x
{\displaystyle x}
. Demonstrate the procedure using the examples given there, or others you may consider lucky.
Par... | #FreeBASIC | FreeBASIC | Const base10 = 10
Dim As Integer c1 = 0, c2 = 0, k = 1
For k = 1 To base10^2
c1 += 1
If (k Mod (base10-1) = (k*k) Mod (base10-1)) Then c2 += 1: Print k;" ";
Next k
Print
Print Using "Intentar ## numeros en lugar de ### numeros ahorra un ##.##%"; c2; c1; 100-(100*c2/c1)
Sleep |
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface | Catmull–Clark subdivision surface | Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals.
The proce... | #OCaml | OCaml | open Dynar
let add3 (x1, y1, z1) (x2, y2, z2) (x3, y3, z3) =
( (x1 +. x2 +. x3),
(y1 +. y2 +. y3),
(z1 +. z2 +. z3) )
let mul m (x,y,z) = (m *. x, m *. y, m *. z)
let avg pts =
let n, (x,y,z) =
List.fold_left
(fun (n, (xt,yt,zt)) (xi,yi,zi) ->
succ n, (xt +. xi, yt +. yi, zt +. zi))... |
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes | Carmichael 3 strong pseudoprimes | A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it.
The Miller Rabin Test uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this.
The purpose of this task is to investigate such numbers using a met... | #Clojure | Clojure |
(ns example
(:gen-class))
(defn prime? [n]
" Prime number test (using Java) "
(.isProbablePrime (biginteger n) 16))
(defn carmichael [p1]
" Triplets of Carmichael primes, with first element prime p1 "
(if (prime? p1)
(into [] (for [h3 (range 2 p1)
:let [g (+ h3 p1)]
d (range 1 g)... |
http://rosettacode.org/wiki/Catamorphism | Catamorphism | Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Task
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
See also
Wikipedia article: ... | #D | D | void main() {
import std.stdio, std.algorithm, std.range, std.meta, std.numeric,
std.conv, std.typecons;
auto list = iota(1, 11);
alias ops = AliasSeq!(q{a + b}, q{a * b}, min, max, gcd);
foreach (op; ops)
writeln(op.stringof, ": ", list.reduce!op);
// std.algorithm.reduce s... |
http://rosettacode.org/wiki/Chaocipher | Chaocipher | Description
The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010.
The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here.
... | #Python | Python | # Python3 implementation of Chaocipher
# left wheel = ciphertext wheel
# right wheel = plaintext wheel
def main():
# letters only! makealpha(key) helps generate lalpha/ralpha.
lalpha = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
ralpha = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
msg = "WELLDONEISBETTERTHANWELLSAID"
print... |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #Kotlin | Kotlin | // version 1.1.2
import java.math.BigInteger
val ONE = BigInteger.ONE
fun pascal(n: Int, k: Int): BigInteger {
if (n == 0 || k == 0) return ONE
val num = (k + 1..n).fold(ONE) { acc, i -> acc * BigInteger.valueOf(i.toLong()) }
val den = (2..n - k).fold(ONE) { acc, i -> acc * BigInteger.valueOf(i.toLong... |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #J | J | NB. These variables are all different
dog=: 'Benjamin'
Dog=: 'Samba'
DOG=: 'Bernie'
'The three dogs are named ',dog,', ',Dog,', and ',DOG
The three dogs are named Benjamin, Samba, and Bernie |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #Java | Java | String dog = "Benjamin";
String Dog = "Samba"; //in general, identifiers that start with capital letters are class names
String DOG = "Bernie"; //in general, identifiers in all caps are constants
//the conventions listed in comments here are not enforced by the language
System.out.println("There are three dogs named " ... |
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists | Cartesian product of two or more lists | Task
Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language.
Demonstrate that your function/method correctly returns:
{1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)}
and, in contrast:
{3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)}
Also demonstrate, using y... | #Delphi | Delphi |
program Cartesian_product_of_two_or_more_lists;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TList = TArray<Integer>;
TLists = TArray<TList>;
TListHelper = record helper for TList
function ToString: string;
end;
TListsHelper = record helper for TLists
function ToString(BreakLines: boo... |
http://rosettacode.org/wiki/Catalan_numbers | Catalan numbers | Catalan numbers
You are encouraged to solve this task according to the task description, using any language you may know.
Catalan numbers are a sequence of numbers which can be defined directly:
C
n
=
1
n
+
1
(
2
n
n
)
=
(
2
n
)
!
(
n
+
1
)
!
n
!
for
n
≥
0.
{\displaystyle C... | #AWK | AWK | # syntax: GAWK -f CATALAN_NUMBERS.AWK
BEGIN {
for (i=0; i<=15; i++) {
printf("%2d %10d\n",i,catalan(i))
}
exit(0)
}
function catalan(n, ans) {
if (n == 0) {
ans = 1
}
else {
ans = ((2*(2*n-1))/(n+1))*catalan(n-1)
}
return(ans)
} |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #Ada | Ada | package My_Class is
type Object is tagged private;
procedure Primitive(Self: Object); -- primitive subprogram
procedure Dynamic(Self: Object'Class);
procedure Static;
private
type Object is tagged null record;
end My_Class; |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #Apex | Apex | // Static
MyClass.method(someParameter);
// Instance
myInstance.method(someParameter); |
http://rosettacode.org/wiki/Cantor_set | Cantor set | Task
Draw a Cantor set.
See details at this Wikipedia webpage: Cantor set
| #ALGOL_W | ALGOL W | begin
% draw a Cantor Set using ASCII %
integer LINES; % number of lines for the set %
integer setWidth; % width of each line of the set %
% we must choose the line width so that the width of each segment i... |
http://rosettacode.org/wiki/Cantor_set | Cantor set | Task
Draw a Cantor set.
See details at this Wikipedia webpage: Cantor set
| #AppleScript | AppleScript | ------------------------- CANTOR SET -----------------------
-- cantor :: [String] -> [String]
on cantor(xs)
script go
on |λ|(s)
set m to (length of s) div 3
set blocks to text 1 thru m of s
if "█" = text 1 of s then
{blocks, replicate(m, space), block... |
http://rosettacode.org/wiki/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #J | J | NB. 2D convolution, filtering, ...
convolve =: 4 : 'x apply (($x) partition y)'
partition=: 2 1 3 0 |: {:@[ ]\ 2 1 0 |: {.@[ ]\ ]
apply=: [: +/ [: +/ *
max3x3 =: 3 : '(0<1{1{y) * (>./>./y)'
addborder =: (0&,@|:@|.)^:4
normalize =: ]%+/@,
attach =: 3 : 'max3x3 (3 3 partition (addborder y))'
unique =: 3 : 'y*i.$y'
con... |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #Common_Lisp | Common Lisp | (defun ip->bit-vector (ip)
(flet ((int->bits (int)
(loop :for i :below 8
:collect (if (logbitp i int) 1 0) :into bits
:finally (return (nreverse bits)))))
(loop :repeat 4
:with start := 0
:for pos := (position #\. ip :start start)
:collect... |
http://rosettacode.org/wiki/Casting_out_nines | Casting out nines | Task (in three parts)
Part 1
Write a procedure (say
c
o
9
(
x
)
{\displaystyle {\mathit {co9}}(x)}
) which implements Casting Out Nines as described by returning the checksum for
x
{\displaystyle x}
. Demonstrate the procedure using the examples given there, or others you may consider lucky.
Par... | #Free_Pascal | Free Pascal | program castout9;
{$ifdef fpc}{$mode delphi}{$endif}
uses generics.collections;
type
TIntegerList = TSortedList<integer>;
procedure co9(const start,base,lim:integer;kaprekars:array of integer);
var
C1:integer = 0;
C2:integer = 0;
S:TIntegerlist;
k,i:integer;
begin
S:=TIntegerlist.Create;
for k := start ... |
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface | Catmull–Clark subdivision surface | Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals.
The proce... | #Phix | Phix | -- demo\rosetta\Catmull_Clark_subdivision_surface.exw
with javascript_semantics
function newPoint() return {0,0,0} end function
function newPointEx() return {newPoint(),0} end function
function centerPoint(sequence p1, p2)
return sq_div(sq_add(p1, p2), 2)
end function
function getFacePoints(sequence inputPoints... |
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes | Carmichael 3 strong pseudoprimes | A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it.
The Miller Rabin Test uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this.
The purpose of this task is to investigate such numbers using a met... | #D | D | enum mod = (in int n, in int m) pure nothrow @nogc=> ((n % m) + m) % m;
bool isPrime(in uint n) pure nothrow @nogc {
if (n == 2 || n == 3)
return true;
else if (n < 2 || n % 2 == 0 || n % 3 == 0)
return false;
for (uint div = 5, inc = 2; div ^^ 2 <= n;
div += inc, inc = 6 - inc)
if (n % div == ... |
http://rosettacode.org/wiki/Catamorphism | Catamorphism | Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Task
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
See also
Wikipedia article: ... | #DCL | DCL | $ list = "1,2,3,4,5"
$ call reduce list "+"
$ show symbol result
$
$ numbers = "5,4,3,2,1"
$ call reduce numbers "-"
$ show symbol result
$
$ call reduce list "*"
$ show symbol result
$ exit
$
$ reduce: subroutine
$ local_list = 'p1
$ value = f$integer( f$element( 0, ",", local_list ))
$ i = 1
$ loop:
$ element = f$el... |
http://rosettacode.org/wiki/Chaocipher | Chaocipher | Description
The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010.
The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here.
... | #Raku | Raku | my @left;
my @right;
sub reset {
@left = <HXUCZVAMDSLKPEFJRIGTWOBNYQ>.comb;
@right = <PTLNBQDEOYSFAVZKGJRIHWXUMC>.comb;
}
sub encode ($letter) {
my $index = @right.first: $letter.uc, :k;
my $enc = @left[$index];
$index.&permute;
$enc
}
sub decode ($letter) {
my $index = @left.first:... |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #Lua | Lua | function nextrow (t)
local ret = {}
t[0], t[#t + 1] = 0, 0
for i = 1, #t do ret[i] = t[i - 1] + t[i] end
return ret
end
function catalans (n)
local t, middle = {1}
for i = 1, n do
middle = math.ceil(#t / 2)
io.write(t[middle] - (t[middle + 1] or 0) .. " ")
t = nextrow(n... |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #JavaScript | JavaScript | var dog = "Benjamin";
var Dog = "Samba";
var DOG = "Bernie";
document.write("The three dogs are named " + dog + ", " + Dog + ", and " + DOG + "."); |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #jq | jq | def task(dog; Dog; DOG):
"The three dogs are named \(dog), \(Dog), and \(DOG)." ;
task("Benjamin"; "Samba"; "Bernie") |
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists | Cartesian product of two or more lists | Task
Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language.
Demonstrate that your function/method correctly returns:
{1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)}
and, in contrast:
{3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)}
Also demonstrate, using y... | #F.23 | F# |
//Nigel Galloway February 12th., 2018
let cP2 n g = List.map (fun (n,g)->[n;g]) (List.allPairs n g)
|
http://rosettacode.org/wiki/Catalan_numbers | Catalan numbers | Catalan numbers
You are encouraged to solve this task according to the task description, using any language you may know.
Catalan numbers are a sequence of numbers which can be defined directly:
C
n
=
1
n
+
1
(
2
n
n
)
=
(
2
n
)
!
(
n
+
1
)
!
n
!
for
n
≥
0.
{\displaystyle C... | #BASIC | BASIC | DECLARE FUNCTION catalan (n AS INTEGER) AS SINGLE
REDIM SHARED results(0) AS SINGLE
FOR x% = 1 TO 15
PRINT x%, catalan (x%)
NEXT
FUNCTION catalan (n AS INTEGER) AS SINGLE
IF UBOUND(results) < n THEN REDIM PRESERVE results(n)
IF 0 = n THEN
results(0) = 1
ELSE
results(n) = ((2 * ((2 * n)... |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #AutoHotkey | AutoHotkey | class myClass
{
Method(someParameter){
MsgBox % SomeParameter
}
}
myClass.method("hi")
myInstance := new myClass
myInstance.Method("bye") |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #Bracmat | Bracmat | ( ( myClass
= (name=aClass)
( Method
= .out$(str$("Output from " !(its.name) ": " !arg))
)
(new=.!arg:?(its.name))
)
& (myClass.Method)$"Example of calling a 'class' method"
& new$(myClass,object1):?MyObject
& (MyObject..Method)$"Example of calling an instance method"
& !MyObject:?Alias
& ... |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #C | C |
#include<stdlib.h>
#include<stdio.h>
typedef struct{
int x;
int (*funcPtr)(int);
}functionPair;
int factorial(int num){
if(num==0||num==1)
return 1;
else
return num*factorial(num-1);
}
int main(int argc,char** argv)
{
functionPair response;... |
http://rosettacode.org/wiki/Call_a_function_in_a_shared_library | Call a function in a shared library | Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.
This is a special case of calling a foreign language function where the focus is c... | #Ada | Ada | with Ada.Text_IO; use Ada.Text_IO;
with Interfaces; use Interfaces;
with Interfaces.C; use Interfaces.C;
with System; use System;
with Ada.Unchecked_Conversion;
procedure Shared_Library_Call is
--
-- Interface to kernel32.dll which is responsible for loading DLLs under Windows.
-- There are re... |
http://rosettacode.org/wiki/Cantor_set | Cantor set | Task
Draw a Cantor set.
See details at this Wikipedia webpage: Cantor set
| #Arturo | Arturo | width: 81
height: 5
lines: array.of: height repeat `*` width
cantor: function [start length idx].export:[lines][
seg: length / 3
if seg = 0 -> return null
loop idx..dec height 'i [
loop (start + seg).. dec start + 2 * seg 'j
-> set lines\[i] j ` `
]
cantor start seg idx+1
... |
http://rosettacode.org/wiki/Calkin-Wilf_sequence | Calkin-Wilf sequence | The Calkin-Wilf sequence contains every nonnegative rational number exactly once.
It can be calculated recursively as follows:
a1 = 1
an+1 = 1/(2⌊an⌋+1-an) for n > 1
Task part 1
Show on this page terms 1 through 20 of the Calkin-Wilf sequence.
To avoid floating point error, you may want to ... | #11l | 11l | T CalkinWilf
n = 1
d = 1
F ()()
V r = (.n, .d)
.n = 2 * (.n I/ .d) * .d + .d - .n
swap(&.n, &.d)
R r
print(‘The first 20 terms of the Calkwin-Wilf sequence are:’)
V cw = CalkinWilf()
[String] seq
L 20
V (n, d) = cw()
seq.append(I d == 1 {String(n)} E n‘/’d)
print(seq.join(‘, ’... |
http://rosettacode.org/wiki/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #Java | Java | import java.awt.image.BufferedImage;
import java.util.Arrays;
/**
* <p><em>This software has been released into the public domain.
* <strong>Please read the notes in this source file for additional information.
* </strong></em></p>
*
* <p>This class provides a configurable implementation of the Canny edge
* de... |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #Factor | Factor | USING: command-line formatting grouping io kernel math.parser
namespaces prettyprint sequences splitting ;
IN: rosetta-code.canonicalize-cidr
! canonicalize a CIDR block: make sure none of the host bits are set
command-line get [ lines ] when-empty
[
! ( CIDR-IP -- bits-in-network-part dotted-decimal )
"/" sp... |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #Go | Go | package main
import (
"fmt"
"log"
"strconv"
"strings"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
// canonicalize a CIDR block: make sure none of the host bits are set
func canonicalize(cidr string) string {
// dotted-decimal / bits in network part
split :=... |
http://rosettacode.org/wiki/Casting_out_nines | Casting out nines | Task (in three parts)
Part 1
Write a procedure (say
c
o
9
(
x
)
{\displaystyle {\mathit {co9}}(x)}
) which implements Casting Out Nines as described by returning the checksum for
x
{\displaystyle x}
. Demonstrate the procedure using the examples given there, or others you may consider lucky.
Par... | #Go | Go | package main
import (
"fmt"
"log"
"strconv"
)
// A casting out nines algorithm.
// Quoting from: http://mathforum.org/library/drmath/view/55926.html
/*
First, for any number we can get a single digit, which I will call the
"check digit," by repeatedly adding the digits. That is, we add the
digits of... |
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface | Catmull–Clark subdivision surface | Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals.
The proce... | #Python | Python |
"""
Input and output are assumed to be in this form based on the talk
page for the task:
input_points = [
[-1.0, 1.0, 1.0],
[-1.0, -1.0, 1.0],
[ 1.0, -1.0, 1.0],
[ 1.0, 1.0, 1.0],
[ 1.0, -1.0, -1.0],
[ 1.0, 1.0, -1.0],
[-1.0, -1.0, -1.0],
[-1.0, 1.0, -1.0]
]
input_faces = [
[0, 1, 2, 3... |
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes | Carmichael 3 strong pseudoprimes | A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it.
The Miller Rabin Test uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this.
The purpose of this task is to investigate such numbers using a met... | #EchoLisp | EchoLisp |
;; charmichaël numbers up to N-th prime ; 61 is 18-th prime
(define (charms (N 18) local: (h31 0) (Prime2 0) (Prime3 0))
(for* ((Prime1 (primes N))
(h3 (in-range 1 Prime1))
(d (+ h3 Prime1)))
(set! h31 (+ h3 Prime1))
#:continue (!zero? (modulo (* h31 (1- Prime1)) d))
#:continue (!= (m... |
http://rosettacode.org/wiki/Catamorphism | Catamorphism | Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Task
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
See also
Wikipedia article: ... | #D.C3.A9j.C3.A0_Vu | Déjà Vu | reduce f lst init:
if lst:
f reduce @f lst init pop-from lst
else:
init
!. reduce @+ [ 1 10 200 ] 4
!. reduce @- [ 1 10 200 ] 4
|
http://rosettacode.org/wiki/Catamorphism | Catamorphism | Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Task
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
See also
Wikipedia article: ... | #Delphi | Delphi |
;; rem : the foldX family always need an initial value
;; fold left a list
(foldl + 0 (iota 10)) ;; 0 + 1 + .. + 9
→ 45
;; fold left a sequence
(lib 'sequences)
(foldl * 1 [ 1 .. 10])
→ 362880 ;; 10!
;; folding left and right
(foldl / 1 ' ( 1 2 3 4))
→ 8/3
(foldr / 1 '(1 2 3 4))
→ 3/8
;;scanl gi... |
http://rosettacode.org/wiki/Chaocipher | Chaocipher | Description
The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010.
The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here.
... | #Ruby | Ruby | txt = "WELLDONEISBETTERTHANWELLSAID"
@left = "HXUCZVAMDSLKPEFJRIGTWOBNYQ".chars
@right = "PTLNBQDEOYSFAVZKGJRIHWXUMC".chars
def encrypt(char)
coded_char = @left[@right.index(char)]
@left.rotate!(@left.index(coded_char))
part = @left.slice!(1,13).rotate
@left.insert(1, *part)
@right.rotate!(@right.in... |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #M2000_Interpreter | M2000 Interpreter |
Module CatalanNumbers {
Def Integer count, t_row, size=31
Dim triangle(1 to size, 1 to size)
\\ call sub
pascal_triangle(size, &triangle())
Print "The first 15 Catalan numbers are"
count = 1% : t_row = 2%
Do {
Print Format$("{0:0:-3}:{1:0:-15}", count, tr... |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #Maple | Maple | catalan:=proc(n)
local i,a:=[1],C:=[1];
for i to n do
a:=[0,op(a)]+[op(a),0];
a:=[0,op(a)]+[op(a),0];
C:=[op(C),a[i+1]-a[i]];
od;
C
end:
catalan(10);
# [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796] |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #Julia | Julia | dog, Dog, DOG = "Benjamin", "Samba", "Bernie"
if dog === Dog
println("There is only one dog, ", DOG)
else
println("The three dogs are: ", dog, ", ", Dog, " and ", DOG)
end |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #K | K |
dog: "Benjamin"
Dog: "Samba"
DOG: "Bernie"
"There are three dogs named ",dog,", ",Dog," and ",DOG
"There are three dogs named Benjamin, Samba and Bernie"
|
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists | Cartesian product of two or more lists | Task
Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language.
Demonstrate that your function/method correctly returns:
{1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)}
and, in contrast:
{3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)}
Also demonstrate, using y... | #Factor | Factor | IN: scratchpad { 1 2 } { 3 4 } cartesian-product .
{ { { 1 3 } { 1 4 } } { { 2 3 } { 2 4 } } }
IN: scratchpad { 3 4 } { 1 2 } cartesian-product .
{ { { 3 1 } { 3 2 } } { { 4 1 } { 4 2 } } }
IN: scratchpad { 1 2 } { } cartesian-product .
{ { } { } }
IN: scratchpad { } { 1 2 } cartesian-product .
{ } |
http://rosettacode.org/wiki/Catalan_numbers | Catalan numbers | Catalan numbers
You are encouraged to solve this task according to the task description, using any language you may know.
Catalan numbers are a sequence of numbers which can be defined directly:
C
n
=
1
n
+
1
(
2
n
n
)
=
(
2
n
)
!
(
n
+
1
)
!
n
!
for
n
≥
0.
{\displaystyle C... | #BASIC256 | BASIC256 | function factorial(n)
if n = 0 then return 1
return n * factorial(n - 1)
end function
function catalan1(n)
prod = 1
for i = n + 2 to 2 * n
prod *= i
next i
return int(prod / factorial(n))
end function
function catalan2(n)
if n = 0 then return 1
sum = 0
for i = 0 to n - 1
... |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #C.2B.2B | C++ | // Static
MyClass::method(someParameter);
// Instance
myInstance.method(someParameter);
// Pointer
MyPointer->method(someParameter);
|
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #C_sharp | C_sharp | // Static
MyClass.Method(someParameter);
// Instance
myInstance.Method(someParameter); |
http://rosettacode.org/wiki/Call_a_function_in_a_shared_library | Call a function in a shared library | Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.
This is a special case of calling a foreign language function where the focus is c... | #Arturo | Arturo | getCurlVersion: function [][
try? [
call.external:'curl "curl_version" .expect: :string []
]
else [
"library not found"
]
]
print ["curl version:" getCurlVersion] |
http://rosettacode.org/wiki/Call_a_function_in_a_shared_library | Call a function in a shared library | Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.
This is a special case of calling a foreign language function where the focus is c... | #AutoHotkey | AutoHotkey | ahkdll := DllCall("LoadLibrary", "str", "AutoHotkey.dll")
clientHandle := DllCall("AutoHotkey\ahkdll", "str", "dllclient.ahk", "str"
, "", "str", "parameter1 parameter2", "Cdecl Int") |
http://rosettacode.org/wiki/Cantor_set | Cantor set | Task
Draw a Cantor set.
See details at this Wikipedia webpage: Cantor set
| #AWK | AWK |
# syntax: GAWK -f CANTOR_SET.AWK
# converted from C
BEGIN {
WIDTH = 81
HEIGHT = 5
for (i=0; i<HEIGHT; ++i) {
for (j=0; j<WIDTH; ++j) {
lines[i][j] = "*"
}
}
cantor(0,WIDTH,1)
for (i=0; i<HEIGHT; ++i) {
for (j=0; j<WIDTH; ++j) {
printf("%s",lines[i][j])
}... |
http://rosettacode.org/wiki/Calkin-Wilf_sequence | Calkin-Wilf sequence | The Calkin-Wilf sequence contains every nonnegative rational number exactly once.
It can be calculated recursively as follows:
a1 = 1
an+1 = 1/(2⌊an⌋+1-an) for n > 1
Task part 1
Show on this page terms 1 through 20 of the Calkin-Wilf sequence.
To avoid floating point error, you may want to ... | #ALGOL_68 | ALGOL 68 | BEGIN
# Show elements 1-20 of the Calkin-Wilf sequence as rational numbers #
# also show the position of a specific element in the seuence #
# Uses code from the Arithmetic/Rational #
# & Continued fraction/Arithmetic/Construct from rational number... |
http://rosettacode.org/wiki/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #Julia | Julia | using Images
canny_edges = canny(img, sigma = 1.4, upperThreshold = 0.80, lowerThreshold = 0.20) |
http://rosettacode.org/wiki/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #Mathematica.2FWolfram_Language | Mathematica/Wolfram Language | Export["out.bmp", EdgeDetect[Import[InputString[]]]]; |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #Hare | Hare | use fmt;
use net::ip;
use strings;
export fn main() void = {
const array = ["87.70.141.1/22",
"36.18.154.103/12",
"62.62.197.11/29",
"67.137.119.181/4",
"161.214.74.21/24",
"184.232.176.184/18"];
for (let i = 0z; i < len(array); i += 1) {
match (canonicalizecidr(array[i])) {
case let s: str =>
fm... |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #Haskell | Haskell | import Control.Monad (guard)
import Data.Bits ((.|.), (.&.), complement, shiftL, shiftR, zeroBits)
import Data.Maybe (listToMaybe)
import Data.Word (Word32, Word8)
import Text.ParserCombinators.ReadP (ReadP, char, readP_to_S)
import Text.Printf (printf)
import Text.Read.Lex (readDecP)
-- A 32-bit IPv4 address, with a... |
http://rosettacode.org/wiki/Casting_out_nines | Casting out nines | Task (in three parts)
Part 1
Write a procedure (say
c
o
9
(
x
)
{\displaystyle {\mathit {co9}}(x)}
) which implements Casting Out Nines as described by returning the checksum for
x
{\displaystyle x}
. Demonstrate the procedure using the examples given there, or others you may consider lucky.
Par... | #Haskell | Haskell | co9 n
| n <= 8 = n
| otherwise = co9 $ sum $ filter (/= 9) $ digits 10 n
task2 = filter (\n -> co9 n == co9 (n ^ 2)) [1 .. 100]
task3 k = filter (\n -> n `mod` k == n ^ 2 `mod` k) [1 .. 100] |
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface | Catmull–Clark subdivision surface | Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals.
The proce... | #Rust | Rust |
pub struct Vector3 {pub x: f64, pub y: f64, pub z: f64, pub w: f64}
pub struct Triangle {pub r: [usize; 3], pub(crate) col: [f32; 4], pub(crate) p: [Vector3; 3], n: Vector3, pub t: [Vector2; 3]}
pub struct Mesh{pub v: Vec<Vector3>, pub f: Vec<Triangle>}
impl Triangle{
pub fn new() -> Triangle {return Triangle... |
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes | Carmichael 3 strong pseudoprimes | A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it.
The Miller Rabin Test uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this.
The purpose of this task is to investigate such numbers using a met... | #F.23 | F# |
// Carmichael Number . Nigel Galloway: November 19th., 2017
let fN n = Seq.collect ((fun g->(Seq.map(fun e->(n,1+(n-1)*(n+g)/e,g,e))){1..(n+g-1)})){2..(n-1)}
let fG (P1,P2,h3,d) =
let mod' n g = (n%g+g)%g
let fN P3 = if isPrime P3 && (P2*P3)%(P1-1)=1 then Some (P1,P2,P3) else None
if isPrime P2 && ((h3+P1)*(P1-... |
http://rosettacode.org/wiki/Catamorphism | Catamorphism | Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Task
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
See also
Wikipedia article: ... | #EchoLisp | EchoLisp |
;; rem : the foldX family always need an initial value
;; fold left a list
(foldl + 0 (iota 10)) ;; 0 + 1 + .. + 9
→ 45
;; fold left a sequence
(lib 'sequences)
(foldl * 1 [ 1 .. 10])
→ 362880 ;; 10!
;; folding left and right
(foldl / 1 ' ( 1 2 3 4))
→ 8/3
(foldr / 1 '(1 2 3 4))
→ 3/8
;;scanl gi... |
http://rosettacode.org/wiki/Chaocipher | Chaocipher | Description
The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010.
The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here.
... | #Rust | Rust | const LEFT_ALPHABET_CT: &str = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
const RIGHT_ALPHABET_PT: &str = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
const ZENITH: usize = 0;
const NADIR: usize = 12;
const SEQUENCE: &str = "WELLDONEISBETTERTHANWELLSAID";
fn cipher(letter: &char, left: &String, right: &String) -> (usize, char) {
let pos = r... |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | nextrow[lastrow_] := Module[{output},
output = ConstantArray[1, Length[lastrow] + 1];
Do[
output[[i + 1]] = lastrow[[i]] + lastrow[[i + 1]];
, {i, 1, Length[lastrow] - 1}];
output
]
pascaltriangle[size_] := NestList[nextrow, {1}, size]
catalannumbers[length_] := Module[{output, basetriangle},
basetria... |
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle | Catalan numbers/Pascal's triangle | Task
Print out the first 15 Catalan numbers by extracting them from Pascal's triangle.
See
Catalan Numbers and the Pascal Triangle. This method enables calculation of Catalan Numbers using only addition and subtraction.
Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl... | #MATLAB_.2F_Octave | MATLAB / Octave | n = 15;
p = pascal(n + 2);
p(n + 4 : n + 3 : end - 1)' - diag(p, 2) |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #Kotlin | Kotlin | fun main(args: Array<String>) {
val dog = "Benjamin"
val Dog = "Samba"
val DOG = "Bernie"
println("The three dogs are named $dog, $Dog and $DOG")
} |
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers | Case-sensitivity of identifiers | Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output:
The three dogs are named Benjamin, Samba and Bernie.
For a language... | #Lasso | Lasso |
local(dog = 'Benjamin')
local(Dog = 'Samba')
local(DOG = 'Bernie')
stdoutnl('There is just one dog named ' + #dog)
|
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists | Cartesian product of two or more lists | Task
Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language.
Demonstrate that your function/method correctly returns:
{1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)}
and, in contrast:
{3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)}
Also demonstrate, using y... | #FreeBASIC | FreeBASIC | #define MAXLEN 64
type listitem ' An item of a list may be a number
is_num as boolean ' or another list, so I have to account
union ' for both, implemented as a union.
list as any ptr ' FreeBASIC is twitchy about circularly
num as uinteger ... |
http://rosettacode.org/wiki/Catalan_numbers | Catalan numbers | Catalan numbers
You are encouraged to solve this task according to the task description, using any language you may know.
Catalan numbers are a sequence of numbers which can be defined directly:
C
n
=
1
n
+
1
(
2
n
n
)
=
(
2
n
)
!
(
n
+
1
)
!
n
!
for
n
≥
0.
{\displaystyle C... | #BBC_BASIC | BBC BASIC | 10 FOR i% = 1 TO 15
20 PRINT FNcatalan(i%)
30 NEXT
40 END
50 DEF FNcatalan(n%)
60 IF n% = 0 THEN = 1
70 = 2 * (2 * n% - 1) * FNcatalan(n% - 1) / (n% + 1) |
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #ChucK | ChucK |
MyClass myClassObject;
myClassObject.myFunction(some parameter);
|
http://rosettacode.org/wiki/Call_an_object_method | Call an object method | In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.
Show how to call a static or class method, and an instance m... | #Clojure | Clojure | (Long/toHexString 15) ; use forward slash for static methods
(System/currentTimeMillis)
(.equals 1 2) ; use dot operator to call instance methods
(. 1 (equals 2)) ; alternative style |
http://rosettacode.org/wiki/Call_a_function_in_a_shared_library | Call a function in a shared library | Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.
This is a special case of calling a foreign language function where the focus is c... | #BaCon | BaCon | ' Call a dynamic library function
PROTO j0
bessel0 = j0(1.0)
PRINT bessel0
|
http://rosettacode.org/wiki/Call_a_function_in_a_shared_library | Call a function in a shared library | Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.
This is a special case of calling a foreign language function where the focus is c... | #BBC_BASIC | BBC BASIC | SYS "MessageBox", @hwnd%, "This is a test message", 0, 0
|
http://rosettacode.org/wiki/Cantor_set | Cantor set | Task
Draw a Cantor set.
See details at this Wikipedia webpage: Cantor set
| #BASIC | BASIC | 10 DEFINT A-Z
20 N = 4
30 W = 3^(N-1)
40 S = W
50 L$ = STRING$(W, "#")
60 PRINT L$
70 IF S = 1 THEN END
80 S = S\3
90 P = 1
100 IF P >= W-S GOTO 60
110 P = P+S
120 MID$(L$,P,S) = SPACE$(S)
130 P = P+S
140 GOTO 100 |
http://rosettacode.org/wiki/Calkin-Wilf_sequence | Calkin-Wilf sequence | The Calkin-Wilf sequence contains every nonnegative rational number exactly once.
It can be calculated recursively as follows:
a1 = 1
an+1 = 1/(2⌊an⌋+1-an) for n > 1
Task part 1
Show on this page terms 1 through 20 of the Calkin-Wilf sequence.
To avoid floating point error, you may want to ... | #AppleScript | AppleScript | -- Return the first n terms of the sequence. Tree generation. Faster for this purpose.
on CalkinWilfSequence(n)
script o
property sequence : {{1, 1}} -- Initialised with the first term ({numerator, denominator}).
end script
-- Work through the growing sequence list, adding the two children of each... |
http://rosettacode.org/wiki/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #MATLAB_.2F_Octave | MATLAB / Octave | BWImage = edge(GrayscaleImage,'canny'); |
http://rosettacode.org/wiki/Canny_edge_detector | Canny edge detector | Task
Write a program that performs so-called canny edge detection on an image.
A possible algorithm consists of the following steps:
Noise reduction. May be performed by Gaussian filter.
Compute intensity gradient (matrices
G
x
{\displaystyle G_{x}}
and
G
y
{\displaystyle G_{y}}
) ... | #Nim | Nim | import lenientops
import math
import nimPNG
const MaxBrightness = 255
type Pixel = int16 # Used instead of byte to be able to store negative values.
#---------------------------------------------------------------------------------------------------
func convolution*[normalize: static bool](input: seq[Pixel]... |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #J | J | cidr=: {{
'a e'=. 0 ".each (y rplc'. ')-.&;:'/'
('/',":e),~rplc&' .'":_8#.\32{.e{.,(8#2)#:a
}} |
http://rosettacode.org/wiki/Canonicalize_CIDR | Canonicalize CIDR | Task
Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form.
That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address.
Example
Given ... | #Java | Java | import java.text.MessageFormat;
import java.text.ParseException;
public class CanonicalizeCIDR {
public static void main(String[] args) {
for (String test : TESTS) {
try {
CIDR cidr = new CIDR(test);
System.out.printf("%-18s -> %s\n", test, cidr.toString());
... |
http://rosettacode.org/wiki/Casting_out_nines | Casting out nines | Task (in three parts)
Part 1
Write a procedure (say
c
o
9
(
x
)
{\displaystyle {\mathit {co9}}(x)}
) which implements Casting Out Nines as described by returning the checksum for
x
{\displaystyle x}
. Demonstrate the procedure using the examples given there, or others you may consider lucky.
Par... | #J | J | castout=: 1 :0
[: (#~ ] =&((m-1)&|) *:) <. + [: i. (+*)@-~
) |
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface | Catmull–Clark subdivision surface | Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals.
The proce... | #Tcl | Tcl | package require Tcl 8.5
# Use math functions and operators as commands (Lisp-like).
namespace path {tcl::mathfunc tcl::mathop}
# Add 3 points.
proc add3 {A B C} {
lassign $A Ax Ay Az
lassign $B Bx By Bz
lassign $C Cx Cy Cz
list [+ $Ax $Bx $Cx] [+ $Ay $By $Cy] [+ $Az $Bz $Cz]
}
# Multiply a point b... |
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes | Carmichael 3 strong pseudoprimes | A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it.
The Miller Rabin Test uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this.
The purpose of this task is to investigate such numbers using a met... | #Factor | Factor | USING: formatting kernel locals math math.primes math.ranges
sequences ;
IN: rosetta-code.carmichael
:: carmichael ( p1 -- )
1 p1 (a,b) [| h3 |
h3 p1 + [1,b) [| d |
h3 p1 + p1 1 - * d mod zero?
p1 neg p1 * h3 rem d h3 mod = and
[
p1 1 - h3 p1 + * d /i 1 ... |
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes | Carmichael 3 strong pseudoprimes | A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it.
The Miller Rabin Test uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this.
The purpose of this task is to investigate such numbers using a met... | #Fortran | Fortran | LOGICAL FUNCTION ISPRIME(N) !Ad-hoc, since N is not going to be big...
INTEGER N !Despite this intimidating allowance of 32 bits...
INTEGER F !A possible factor.
ISPRIME = .FALSE. !Most numbers aren't prime.
DO F = 2,SQRT(DFLOAT(N)) !Wince...
IF (MOD(N,F).EQ.0) RETURN ... |
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.
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.