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/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Ruby | Ruby | def jacobi(a, n)
raise ArgumentError.new "n must b positive and odd" if n < 1 || n.even?
res = 1
until (a %= n) == 0
while a.even?
a >>= 1
res = -res if [3, 5].include? n % 8
end
a, n = n, a
res = -res if [a % 4, n % 4] == [3, 3]
end
n == 1 ? res : 0
end
puts "Jacobian symbols fo... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Rust | Rust | fn jacobi(mut n: i32, mut k: i32) -> i32 {
assert!(k > 0 && k % 2 == 1);
n %= k;
let mut t = 1;
while n != 0 {
while n % 2 == 0 {
n /= 2;
let r = k % 8;
if r == 3 || r == 5 {
t = -t;
}
}
std::mem::swap(&mut n, &mut k... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #TI-83_BASIC | TI-83 BASIC | :"SHUFFLE"
:L1→L2
:dim(L2)→A
:For(B,1,dim(L2)-1)
:randInt(1,A)→C
:L2(C)→D
:L2(A)→L2(C)
:D→L2(A)
:A-1→A
:End
:DelVar A
:DelVar B
:DelVar C
:DelVar D
:Return
|
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #OCaml | OCaml | let i = ref 42 (* initial value doesn't matter *)
let sum' i lo hi term =
let result = ref 0. in
i := lo;
while !i <= hi do
result := !result +. term ();
incr i
done;
!result
let () =
Printf.printf "%f\n" (sum' i 1 100 (fun () -> 1. /. float !i)) |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Oforth | Oforth | : mysum(lo, hi, term) | i | 0 lo hi for: i [ i term perform + ] ; |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Quackery | Quackery | [ dup
' [ sortwith ]
]'[ nested join
do = ] is jortsortwith ( [ --> b ) |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Racket | Racket | #lang racket/base
(define (jort-sort l [<? <])
(equal? l (sort l <?))) |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #Arturo | Arturo | J: function [n]-> ((2^n) - (neg 1)^n)/3
JL: function [n]-> (2^n) + (neg 1)^n
JO: function [n]-> (J n) * (J n+1)
printFirst: function [label, what, predicate, count][
print ["First" count label++":"]
result: new []
i: 0
while [count > size result][
num: do ~"|what| i"
if do predicate -... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #PL.2FM | PL/M | 100H:
/* FIND JEWELS AMONG STONES */
COUNT$JEWELS: PROCEDURE (JEWELS, STONES) BYTE;
DECLARE (JEWELS, STONES) ADDRESS;
DECLARE (J BASED JEWELS, S BASED STONES) BYTE;
DECLARE JFLAG (256) BYTE, I BYTE;
/* ZERO JEWEL FLAGS */
DO I=0 TO 255;
JFLAG(I) = 0;
END;
/* LOOP THROUGH JEWELS... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Prolog | Prolog |
:- system:set_prolog_flag(double_quotes,codes) .
count_jewels(STONEs0,JEWELs0,COUNT)
:-
findall(X,(member(X,JEWELs0),member(X,STONEs0)),ALLs) ,
length(ALLs,COUNT)
.
|
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Go | Go | package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"sort"
)
func jaroSim(str1, str2 string) float64 {
if len(str1) == 0 && len(str2) == 0 {
return 1
}
if len(str1) == 0 || len(str2) == 0 {
return 0
}
match_distance := len(str1)
if len(str2) > match_dist... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Scala | Scala |
def jacobi(a_p: Int, n_p: Int): Int =
{
var a = a_p
var n = n_p
if (n <= 0) return -1
if (n % 2 == 0) return -1
a %= n
var result = 1
while (a != 0) {
while (a % 2 == 0) {
a /= 2
if (n % 8 == 3 || n % 8 == 5) result = -result
}
val t = a
a = n
... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #TUSCRIPT | TUSCRIPT | $$ MODE TUSCRIPT
oldnumbers=newnumbers="",range=20
LOOP nr=1,#range
oldnumbers=APPEND(oldnumbers,nr)
ENDLOOP
PRINT "before ",oldnumbers
LOOP r=#range,1,-1
RANDNR=RANDOM_NUMBERS (1,#r,1)
shuffle=SELECT (oldnumbers,#randnr,oldnumbers)
newnumbers=APPEND(newnumbers,shuffle)
ENDLOOP
PRINT "after ",newnumbers |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Oz | Oz | declare
fun {Sum I Lo Hi Term}
Temp = {NewCell 0.0}
in
I := Lo
for while:@I =< Hi do
Temp := @Temp + {Term}
I := @I + 1
end
@Temp
end
I = {NewCell unit}
in
{Show {Sum I 1 100 fun {$} 1.0 / {Int.toFloat @I} end}} |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #PARI.2FGP | PARI/GP | program Jensens_Device;
{$IFDEF FPC}
{$MODE objFPC}
{$ENDIF}
type
tTerm = function(i: integer): real;
function term(i: integer): real;
begin
term := 1 / i;
end;
function sum(var i: LongInt; lo, hi: integer; term: tTerm): real;
begin
result := 0;
i := lo;
while i <= hi do
begin
result := result... |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Raku | Raku | sub jort-sort { @_ eqv @_.sort } |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #REXX | REXX | /*REXX program verifies that an array is sorted using a jortSort algorithm. */
parse arg $ /*obtain the list of numbers from C.L. */
if $='' then $=1 2 4 3 /*Not specified? Then use the default.*/
say 'array items=' space($) ... |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #C | C | #include <stdio.h>
#include <gmp.h>
void jacobsthal(mpz_t r, unsigned long n) {
mpz_t s;
mpz_init(s);
mpz_set_ui(r, 1);
mpz_mul_2exp(r, r, n);
mpz_set_ui(s, 1);
if (n % 2) mpz_neg(s, s);
mpz_sub(r, r, s);
mpz_div_ui(r, r, 3);
}
void jacobsthal_lucas(mpz_t r, unsigned long n) {
mp... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Python | Python | def countJewels(s, j):
return sum(x in j for x in s)
print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z") |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #R | R | J_n_S <- function(stones ="aAAbbbb", jewels = "aA") {
stones <- unlist(strsplit(stones, split = "")) # obtain a character vector
jewels <- unlist(strsplit(jewels, split = ""))
count <- sum(stones %in% jewels)
}
print(J_n_S("aAAbbbb", "aA"))
print(J_n_S("ZZ", "z"))
print(J_n_S("lgGKJGljglghGLGHlhglghoIPOgfdtrdD... |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #J | J | jaro=: {{
Eq=. (x=/y)*(<.<:-:x>.&#y)>:|x -/&i.&# y
xM=. (+./"1 Eq)#x
yM=. (+./"2 Eq)#y
M=. xM <.&# yM
T=. -: +/ xM ~:&(M&{.) yM
3%~ (M%#x) + (M%#y) + (M-T)%M
}}
jarowinkler=: {{
p=. 0.1
l=. +/*/\x =&((4<.x<.&#y)&{.) y
simj=. x jaro y
-.simj + l*p*-.simj
}} |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Java | Java | import java.io.*;
import java.util.*;
public class JaroWinkler {
public static void main(String[] args) {
try {
List<String> words = loadDictionary("linuxwords.txt");
String[] strings = {
"accomodate", "definately", "goverment", "occured",
"publicall... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Scheme | Scheme | (define jacobi (lambda (a n)
(let ((a-mod-n (modulo a n)))
(if (zero? a-mod-n)
(if (= n 1)
1
0)
(if (even? a-mod-n)
(case (modulo n 8)
((3 5) (- (jacobi (/ a-mod-n 2) n)))
((1 7) (jacobi (/ a-mod-n 2) n)))
(if (and (= (modulo a-mod-n 4) 3) (= (modulo n 4... |
http://rosettacode.org/wiki/Josephus_problem | Josephus problem | Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n
−
1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
,
removing eve... | #11l | 11l | F j(n, k)
V p = Array(0 .< n)
V i = 0
[Int] seq
L !p.empty
i = (i + k - 1) % p.len
seq.append(p.pop(i))
R "Prisoner killing order: #..\nSurvivor: #.".format(seq[0 .< (len)-1].join(‘, ’), seq.last)
print(j(5, 2))
print(j(41, 3)) |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #uBasic.2F4tH | uBasic/4tH | PRINT "before:"
FOR L = 0 TO 51
@(L) = L
PRINT @(L); " ";
NEXT
FOR L = 51 TO 0 STEP -1
C = RND(L + 1)
IF C # L THEN
PUSH @(C), L, @(L), C
GOSUB 100
ENDIF
NEXT
PRINT : PRINT "after:"
FOR L = 0 TO 51
PRINT @(L); " ";
NEXT
PRINT
END
100 @(POP()) = POP() : @(POP()) = POP() : RETUR... |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Pascal | Pascal | program Jensens_Device;
{$IFDEF FPC}
{$MODE objFPC}
{$ENDIF}
type
tTerm = function(i: integer): real;
function term(i: integer): real;
begin
term := 1 / i;
end;
function sum(var i: LongInt; lo, hi: integer; term: tTerm): real;
begin
result := 0;
i := lo;
while i <= hi do
begin
result := result... |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Ring | Ring |
aList = [4,2,3,1]
see jortSort(aList) + nl
func jortSort array
originalArray = array
array = sort(array)
for i= 1 to len(originalArray)
if originalArray[i] != array[i] return false ok
next
return true
|
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Ruby | Ruby | def jort_sort(array)
array == array.sort
end |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Rust | Rust | use std::cmp::{Ord, Eq};
fn jort_sort<T: Ord + Eq + Clone>(array: Vec<T>) -> bool {
// sort the array
let mut sorted_array = array.to_vec();
sorted_array.sort();
// compare to see if it was originally sorted
for i in 0..array.len() {
if array[i] != sorted_array[i] {
return fa... |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #C.2B.2B | C++ | #include <gmpxx.h>
#include <iomanip>
#include <iostream>
using big_int = mpz_class;
bool is_probably_prime(const big_int& n) {
return mpz_probab_prime_p(n.get_mpz_t(), 30) != 0;
}
big_int jacobsthal_number(unsigned int n) {
return ((big_int(1) << n) - (n % 2 == 0 ? 1 : -1)) / 3;
}
big_int jacobsthal_... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Quackery | Quackery | [ 0 0 rot
witheach [ bit | ]
rot witheach
[ bit over & if
[ dip 1+ ] ]
drop ] is j&s ( $ $ --> n )
$ "aAAbbbb" $ "aA" j&s echo |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Racket | Racket | #lang racket
(define (jewels-and-stones stones jewels)
(length (filter (curryr member (string->list jewels)) (string->list stones))))
(module+ main
(jewels-and-stones "aAAbbbb" "aA")
(jewels-and-stones "ZZ" "z"))
|
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #jq | jq | # See [[Jaro_similarity#jq]] for the implementation of jaro/2
def length_of_common_prefix($s1; $s2):
if ($s1|length) > ($s2|length) then length_of_common_prefix($s2; $s1)
else ($s1|explode) as $x1
| ($s2|explode) as $x2
| first( range(0;$x1|length) | select( $x1[.] != $x2[.] )) // ($x1|length)
end;
# Outp... |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Julia | Julia | # download("http://users.cs.duke.edu/~ola/ap/linuxwords", "linuxwords.txt")
const words = read("linuxwords.txt", String) |> split .|> strip
function jarowinklerdistance(s1, s2)
if length(s1) < length(s2)
s1, s2 = s2, s1
end
len1, len2 = length(s1), length(s2)
len2 == 0 && return 0.0
delta ... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Sidef | Sidef | func jacobi(n, k) {
assert(k > 0, "#{k} must be positive")
assert(k.is_odd, "#{k} must be odd")
var t = 1
while (n %= k) {
var v = n.valuation(2)
t *= (-1)**v if (k%8 ~~ [3,5])
n >>= v
(n,k) = (k,n)
t = -t if ([n%4, k%4] == [3,3])
}
k==1 ? t : 0
}... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Swift | Swift | import Foundation
func jacobi(a: Int, n: Int) -> Int {
var a = a % n
var n = n
var res = 1
while a != 0 {
while a & 1 == 0 {
a >>= 1
if n % 8 == 3 || n % 8 == 5 {
res = -res
}
}
(a, n) = (n, a)
if a % 4 == 3 && n % 4 == 3 {
res = -res
}
a %= n
... |
http://rosettacode.org/wiki/Josephus_problem | Josephus problem | Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n
−
1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
,
removing eve... | #360_Assembly | 360 Assembly | * Josephus problem 10/02/2017
JOSEPH CSECT
USING JOSEPH,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
STM R14,R12,12(R13) prolog
ST R13,4(R15) " <-
ST R15,8... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #UNIX_Shell | UNIX Shell | # Shuffle array[@].
function shuffle {
integer i j t
((i = ${#array[@]}))
while ((i > 1)); do
((j = RANDOM)) # 0 <= j < 32768
((j < 32768 % i)) && continue # no modulo bias
((j %= i)) # 0 <= j < i
((i -= 1))
((t = array[i]))
((array[i] = array[j]))
((array[j] = ... |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Perl | Perl | my $i;
sub sum {
my ($i, $lo, $hi, $term) = @_;
my $temp = 0;
for ($$i = $lo; $$i <= $hi; $$i++) {
$temp += $term->();
}
return $temp;
}
print sum(\$i, 1, 100, sub { 1 / $i }), "\n"; |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Scala | Scala | import java.util.Objects.deepEquals
def jortSort[K:Ordering]( a:Array[K] ) = deepEquals(a.sorted, a) |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Sidef | Sidef | func jort_sort(array) { array == array.sort }; |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #Factor | Factor | USING: grouping io kernel lists lists.lazy math math.functions
math.primes prettyprint sequences ;
: 2^-1^ ( n -- 2^n -1^n ) dup 2^ -1 rot ^ ;
: jacobsthal ( m -- n ) 2^-1^ - 3 / ;
: jacobsthal-lucas ( m -- n ) 2^-1^ + ;
: as-list ( quot -- list ) 0 lfrom swap lmap-lazy ; inline
: jacobsthals ( -- list ) [ jacobsthal... |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #FreeBASIC | FreeBASIC | Function isPrime(n As Ulongint) As Boolean
If n < 2 Then Return False
If n Mod 2 = 0 Then Return false
For i As Uinteger = 3 To Int(Sqr(n))+1 Step 2
If n Mod i = 0 Then Return false
Next i
Return true
End Function
Dim Shared As Uinteger n(1)
Dim Shared As Uinteger i0 = 0, i1 = 1
Dim Share... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Raku | Raku | sub count-jewels ( Str $j, Str $s --> Int ) {
my %counts_of_all = $s.comb.Bag;
my @jewel_list = $j.comb.unique;
return %counts_of_all ∩ @jewel_list.Bag ?? %counts_of_all{ @jewel_list }.sum !! 0;
}
say count-jewels 'aA' , 'aAAbbbb';
say count-jewels 'z' , 'ZZ'; |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Red | Red | Red [
title: "Jewels and stones"
red-version: 0.6.4
]
count: function [
"Returns the number of values in a block for which a function returns true"
values [any-list! string!] "The values from which to count"
fn [function!] "A function that returns true or false"
][
count: 0
foreach value v... |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Mathematica.2FWolfram_Language | Mathematica/Wolfram Language | ClearAll[JWD]
JWD[a_][b_]:=Experimental`JaroWinklerDistance[a,b]
dict=DictionaryLookup[];
TakeSmallestBy[dict->{"Element","Value"},JWD["accomodate"],5]//Grid
TakeSmallestBy[dict->{"Element","Value"},JWD["definately"],5]//Grid
TakeSmallestBy[dict->{"Element","Value"},JWD["goverment"],5]//Grid
TakeSmallestBy[dict->{"Elem... |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Nim | Nim | import lenientops
func jaroSim(s1, s2: string): float =
if s1.len == 0 and s2.len == 0: return 1
if s1.len == 0 or s2.len == 0: return 0
let matchDistance = max(s1.len, s2.len) div 2 - 1
var s1Matches = newSeq[bool](s1.len)
var s2Matches = newSeq[bool](s2.len)
var matches = 0
for i in 0..s1.high:
... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Vlang | Vlang | fn jacobi(aa u64, na u64) ?int {
mut a := aa
mut n := na
if n%2 == 0 {
return error("'n' must be a positive odd integer")
}
a %= n
mut result := 1
for a != 0 {
for a%2 == 0 {
a /= 2
nn := n % 8
if nn == 3 || nn == 5 {
result... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #Wren | Wren | import "/fmt" for Fmt
var jacobi = Fn.new { |a, n|
if (!n.isInteger || n <= 0 || n%2 == 0) {
Fiber.abort("The 'n' parameter must be an odd positive integer.")
}
a = a % n
var result = 1
while (a != 0) {
while (a%2 == 0) {
a = a / 2
var nm8 = n % 8
... |
http://rosettacode.org/wiki/Josephus_problem | Josephus problem | Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n
−
1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
,
removing eve... | #6502_Assembly | 6502 Assembly | JSEPHS: STA $D0 ; n
STX $D1 ; k
LDA #$FF
LDX #$00
SETUP: STA $1000,X ; populate array with hex FF
INX
CPX $D0
BEQ KILL
JMP SETUP
KILL: LDA #$00 ; number killed so far
STA $D2
LDX #$00 ; position within arr... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #Ursala | Ursala | shuffle = @iNX ~&l->r ^jrX/~&l ~&lK8PrC |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Phix | Phix | with javascript_semantics
function sumr(integer lo, hi, rid)
atom res = 0
for i=lo to hi do
res += rid(i)
end for
return res
end function
function reciprocal(atom i) return 1/i end function
?sumr(1, 100, reciprocal)
|
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #PHP | PHP | $i;
function sum (&$i, $lo, $hi, $term) {
$temp = 0;
for ($i = $lo; $i <= $hi; $i++) {
$temp += $term();
}
return $temp;
}
echo sum($i, 1, 100, create_function('', 'global $i; return 1 / $i;')), "\n";
//Output: 5.18737751764 (5.1873775176396)
function sum ($lo,$hi)
{
$temp = 0;
for ($i = $... |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #SSEM | SSEM | 11011000000000100000000000000000 0. -27 to c
00000000000000110000000000000000 1. Test
11101000000000000000000000000000 2. 23 to CI
10011000000001100000000000000000 3. c to 25
10011000000000100000000000000000 4. -25 to c
01011000000000010000000000000000 5. Sub. 26
00000000000000110000000000000000 6. Test
1... |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Swift | Swift | func jortSort<T:Comparable>(array: [T]) -> Bool {
return array == sorted(array)
} |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #Go | Go | package main
import (
"fmt"
"math/big"
)
func jacobsthal(n uint) *big.Int {
t := big.NewInt(1)
t.Lsh(t, n)
s := big.NewInt(1)
if n%2 != 0 {
s.Neg(s)
}
t.Sub(t, s)
return t.Div(t, big.NewInt(3))
}
func jacobsthalLucas(n uint) *big.Int {
t := big.NewInt(1)
t.Lsh(t... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #REXX | REXX | /*REXX pgm counts how many letters (in the 1st string) are in common with the 2nd string*/
say count('aAAbbbb', "aA")
say count('ZZ' , "z" )
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────────... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Ring | Ring | # Project Jewels and Stones
jewels = "aA"
stones = "aAAbbbb"
see jewelsandstones(jewels,stones) + nl
jewels = "z"
stones = "ZZ"
see jewelsandstones(jewels,stones) + nl
func jewelsandstones(jewels,stones)
num = 0
for n = 1 to len(stones)
pos = substr(jewels,stones[n])
if po... |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Perl | Perl | use strict;
use warnings;
use List::Util qw(min max head);
sub jaro_winkler {
my($s, $t) = @_;
my(@s_matches, @t_matches, $matches);
return 0 if $s eq $t;
my $s_len = length $s; my @s = split //, $s;
my $t_len = length $t; my @t = split //, $t;
my $match_distance = int (max($s_len,$t_len... |
http://rosettacode.org/wiki/Jacobi_symbol | Jacobi symbol | The Jacobi symbol is a multiplicative function that generalizes the Legendre symbol. Specifically, the Jacobi symbol (a | n) equals the product of the Legendre symbols (a | p_i)^(k_i), where n = p_1^(k_1)*p_2^(k_2)*...*p_i^(k_i) and the Legendre symbol (a | p) denotes the value of a ^ ((p-1)/2) (mod p)
(a | p) ≡ 1 ... | #zkl | zkl | fcn jacobi(a,n){
if(n.isEven or n<1)
throw(Exception.ValueError("'n' must be a positive odd integer"));
a=a%n; result,t := 1,0;
while(a!=0){
while(a.isEven){
a/=2; n_mod_8:=n%8;
if(n_mod_8==3 or n_mod_8==5) result=-result;
}
t,a,n = a,n,t;
if(a%4==3 and n%4==3) result=-re... |
http://rosettacode.org/wiki/Josephus_problem | Josephus problem | Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n
−
1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
,
removing eve... | #AArch64_Assembly | AArch64 Assembly |
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program josephus64.s */
/* run with josephus64 maxi intervalle */
/* example : josephus64 41 3
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include ... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #VBA | VBA | Private Sub Knuth(Optional ByRef a As Variant)
Dim t As Variant, i As Integer
If Not IsMissing(a) Then
For i = UBound(a) To LBound(a) + 1 Step -1
j = Int((UBound(a) - LBound(a) + 1) * Rnd + LBound(a))
t = a(i)
a(i) = a(j)
a(j) = t
Next i
End If... |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #PicoLisp | PicoLisp | (scl 6)
(de jensen (I Lo Hi Term)
(let Temp 0
(set I Lo)
(while (>= Hi (val I))
(inc 'Temp (Term))
(inc I) )
Temp ) )
(let I (box) # Create indirect reference
(format
(jensen I 1 100 '(() (*/ 1.0 (val I))))
*Scl ) ) |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #PureBasic | PureBasic | Prototype.d func()
Global i
Procedure.d Sum(*i.Integer, lo, hi, *term.func)
Protected Temp.d
For i=lo To hi
temp + *term()
Next
ProcedureReturn Temp
EndProcedure
Procedure.d term_func()
ProcedureReturn 1/i
EndProcedure
Answer.d = Sum(@i, 1, 100, @term_func()) |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Tcl | Tcl |
proc jortsort {args} {
set list [lindex $args end]
set list [list {*}$list] ;# ensure canonical list form
set options [lrange $args 0 end-1]
expr {[lsort {*}$options $list] eq $list}
}
|
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #UNIX_Shell | UNIX Shell |
JortSort() {
cmp -s <(printf “%s\n” “$@“) <(printf “%s\n” “$@“ | sort)
}
JortSortVerbose() {
if JortSort “$@“; then
echo True
else
echo False
If
}
JortSortVerbose 1 2 3 4 5
JortSortVerbose 1 3 4 5 2
JortSortVerbose a b c
JortSortVerbose c a b
|
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #Haskell | Haskell | jacobsthal :: [Integer]
jacobsthal = 0 : 1 : zipWith (\x y -> 2 * x + y) jacobsthal (tail jacobsthal)
jacobsthalLucas :: [Integer]
jacobsthalLucas = 2 : 1 : zipWith (\x y -> 2 * x + y) jacobsthalLucas (tail jacobsthalLucas)
jacobsthalOblong :: [Integer]
jacobsthalOblong = zipWith (*) jacobsthal (tail jacobsthal)
... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Ruby | Ruby | stones, jewels = "aAAbbbb", "aA"
stones.count(jewels) # => 3
|
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Rust | Rust | fn count_jewels(stones: &str, jewels: &str) -> u8 {
let mut count: u8 = 0;
for cur_char in stones.chars() {
if jewels.contains(cur_char) {
count += 1;
}
}
count
}
fn main() {
println!("{}", count_jewels("aAAbbbb", "aA"));
println!("{}", count_jewels("ZZ", "z"));
}
|
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Phix | Phix | function jaro(string str1, str2)
str1 = trim(upper(str1))
str2 = trim(upper(str2))
integer len1 = length(str1),
len2 = length(str2),
match_distance = floor(max(len1,len2)/2)-1,
match_count = 0,
half_transposed = 0
if len1==0 then return len2==0 end if
... |
http://rosettacode.org/wiki/Jaro_similarity | Jaro similarity | The Jaro distance is a measure of edit distance between two strings; its inverse, called the Jaro similarity, is a measure of two strings' similarity: the higher the value, the more similar the strings are. The score is normalized such that 0 equates to no similarities and 1 is an exact match.
Definition
The... | #11l | 11l | F jaro(s, t)
V s_len = s.len
V t_len = t.len
I s_len == 0 & t_len == 0
R 1.0
V match_distance = (max(s_len, t_len) I/ 2) - 1
V s_matches = [0B] * s_len
V t_matches = [0B] * t_len
V matches = 0
V transpositions = 0
L(i) 0 .< s_len
V start = max(0, i - match_distance)
V ... |
http://rosettacode.org/wiki/Josephus_problem | Josephus problem | Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n
−
1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
,
removing eve... | #Ada | Ada | with Ada.Command_Line, Ada.Text_IO;
procedure Josephus is
function Arg(Idx, Default: Positive) return Positive is -- read Argument(Idx)
(if Ada.Command_Line.Argument_Count >= Index
then Positive'Value(Ada.Command_Line.Argument(Index)) else Default);
Prisoners: constant Positive := Arg(Idx =>... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #VBScript | VBScript |
function shuffle( a )
dim i
dim r
randomize timer
for i = lbound( a ) to ubound( a )
r = int( rnd * ( ubound( a ) + 1 ) )
if r <> i then
swap a(i), a(r)
end if
next
shuffle = a
end function
sub swap( byref a, byref b )
dim tmp
tmp = a
a = b
b = tmp
end sub |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Python | Python | class Ref(object):
def __init__(self, value=None):
self.value = value
def harmonic_sum(i, lo, hi, term):
# term is passed by-name, and so is i
temp = 0
i.value = lo
while i.value <= hi: # Python "for" loop creates a distinct which
temp += term() # would not be shared with the pass... |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #R | R | sum <- function(var, lo, hi, term)
eval(substitute({
.temp <- 0;
for (var in lo:hi) {
.temp <- .temp + term
}
.temp
}, as.list(match.call()[-1])),
enclos=parent.frame())
sum(i, 1, 100, 1/i) #prints 5.187378
##and because of enclos=parent.frame(), the term can involve variables in the cal... |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #VBScript | VBScript | Function JortSort(s)
JortSort = True
arrPreSort = Split(s,",")
Set arrSorted = CreateObject("System.Collections.ArrayList")
'Populate the resorted arraylist.
For i = 0 To UBound(arrPreSort)
arrSorted.Add(arrPreSort(i))
Next
arrSorted.Sort()
'Compare the elements of both arrays.
For j = 0 To UBound(arrPreSort... |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Vlang | Vlang |
fn main() {
println(jort_sort([1, 2, 1, 11, 213, 2, 4])) //false
println(jort_sort([0, 1, 0, 0, 0, 0])) //false
println(jort_sort([1, 2, 4, 11, 22, 22])) //true
println(jort_sort([0, 0, 0, 1, 2, 2])) //true
}
fn jort_sort(a []int) bool {
mut c := a.clone()
c.sort()
for k, v in c {
... |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #J | J | ja=: 3 %~ 2x&^ - _1x&^ NB. Jacobsthal
jl=: 2x&^ + _1x&^ NB.Jacobsthal-Lucas |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #jq | jq | # Split the input array into a stream of arrays
def chunks(n):
def c: .[0:n], (if length > n then .[n:]|c else empty end);
c;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
# If $j is 0, then an error condition is raised;
# otherwise, assuming infinite-precision integer arithmetic,
# if... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Scala | Scala | object JewelsStones extends App {
def countJewels(s: String, j: String): Int = s.count(i => j.contains(i))
println(countJewels("aAAbbbb", "aA"))
println(countJewels("ZZ", "z"))
} |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Sidef | Sidef | func countJewels(s, j) {
s.chars.count { |c|
j.contains(c)
}
}
say countJewels("aAAbbbb", "aA") #=> 3
say countJewels("ZZ", "z") #=> 0 |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Python | Python | """
Test Jaro-Winkler distance metric.
linuxwords.txt is from http://users.cs.duke.edu/~ola/ap/linuxwords
"""
WORDS = [s.strip() for s in open("linuxwords.txt").read().split()]
MISSPELLINGS = [
"accomodate",
"definately",
"goverment",
"occured",
"publically",
"recieve",
"seperate",
"... |
http://rosettacode.org/wiki/Jaro_similarity | Jaro similarity | The Jaro distance is a measure of edit distance between two strings; its inverse, called the Jaro similarity, is a measure of two strings' similarity: the higher the value, the more similar the strings are. The score is normalized such that 0 equates to no similarities and 1 is an exact match.
Definition
The... | #Action.21 | Action! |
DEFINE STRING="CHAR ARRAY" ; sys.act
DEFINE ASCII_SpaceBar="32"
INT FUNC JaroDistance(STRING str1, str2)
STRING Z(15)
INT S1, S2, J, M, N, L, I, K, skip, Max, Min
INT Result
Result=0
S1=str1(0)
S2=str2(0)
IF S1>S2 THEN
SCopy(Z,str1)
SCopy(str1,str2)
SCopy(str2,Z)
M=S1
S1=S2
S2=... |
http://rosettacode.org/wiki/Josephus_problem | Josephus problem | Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n
−
1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
,
removing eve... | #ALGOL_68 | ALGOL 68 | BEGIN
PROC josephus = (INT n, k, m) INT :
CO Return m-th on the reversed kill list; m=0 is final survivor. CO
BEGIN
INT lm := m; CO Local copy of m CO
FOR a FROM m+1 WHILE a <= n DO lm := (lm+k) %* a OD;
lm
END;
INT n = 41, k=3;
printf (($"n = ", g(0), ", k = ", g(0), ", final surv... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #Vedit_macro_language | Vedit macro language | // Test main
#90 = Time_Tick // seed for random number generator
#99 = 20 // number of items in the array
IT("Before:") IN
for (#100 = 0; #100 < #99; #100++) {
#@100 = #100
Num_Ins(#@100, LEFT+NOCR) IT(" ")
}
IN
Call("SHUFFLE_NUMBERS")
IT("After:") IN
for (#100 =... |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Racket | Racket |
#lang algol60
begin
integer i;
real procedure sum (i, lo, hi, term);
value lo, hi;
integer i, lo, hi;
real term;
comment term is passed by-name, and so is i;
begin
real temp;
temp := 0;
for i := lo step 1 until hi do
temp := temp + term;
sum := temp
... |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Raku | Raku | sub sum($i is rw, $lo, $hi, &term) {
my $temp = 0;
loop ($i = $lo; $i <= $hi; $i++) {
$temp += term;
}
return $temp;
}
my $i;
say sum $i, 1, 100, { 1 / $i }; |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #Wren | Wren | import "/sort" for Sort
var jortSort = Fn.new { |a|
var b = Sort.merge(a)
for (i in 0...a.count) {
if (a[i] != b[i]) return false
}
return true
}
var tests = [ [1, 2, 3, 4, 5], [2, 1, 3, 4, 5] ]
for (test in tests) System.print("%(test) -> %(jortSort.call(test) ? "sorted" : "not sorted")") |
http://rosettacode.org/wiki/JortSort | JortSort |
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
... | #zkl | zkl | fcn jort(list){ False!=list.reduce(fcn(a,b){ (a>b) and return(Void.Stop,False); b }) } |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #Julia | Julia | using Lazy
using Primes
J(n) = (2^n - (-1)^n) ÷ 3
L(n) = 2^n + (-1)^n
Jacobsthal = @>> Lazy.range(0) map(J)
JLucas = @>> Lazy.range(0) map(L)
Joblong = @>> Lazy.range(big"0") map(n -> J(n) * J(n + 1))
Jprimes = @>> Lazy.range(big"0") map(J) filter(isprime)
function printrows(title, vec, columnsize = 15, columns ... |
http://rosettacode.org/wiki/Jacobsthal_numbers | Jacobsthal numbers | Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1.
J0 = 0
J1 = 1
Jn = Jn-1 + 2 ×... | #Mathematica.2FWolfram_Language | Mathematica/Wolfram Language | ClearAll[Jacobsthal, JacobsthalLucas, JacobsthalOblong]
Jacobsthal[n_]:=(2^n-(-1)^n)/3
JacobsthalLucas[n_]:=2^n+(-1)^n
JacobsthalOblong[n_]:=Jacobsthal[n]Jacobsthal[n+1]
Jacobsthal[Range[0, 29]]
JacobsthalLucas[Range[0, 29]]
JacobsthalOblong[Range[0, 19]]
n=0;
i=0;
Reap[While[n<20,
If[
PrimeQ[Jacobsthal[i]]
,
Sow... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #Snobol | Snobol | * See how many jewels are among the stones
DEFINE('JEWELS(JWL,STN)') :(JEWELS_END)
JEWELS JEWELS = 0
JWL = ANY(JWL)
JMATCH STN JWL = '' :F(RETURN)
JEWELS = JEWELS + 1 :(JMATCH)
JEWELS_END
* Example from the task (prin... |
http://rosettacode.org/wiki/Jewels_and_stones | Jewels and stones | Jewels and stones
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer.
Both strings can contain any number of upper or lower case letters. However, in the case ... | #SQL | SQL | -- See how many jewels are among the stones
DECLARE @S VARCHAR(1024) = 'AaBbCcAa'
, @J VARCHAR(1024) = 'aA';
DECLARE @SLEN INT = len(@S);
DECLARE @JLEN INT = len(@J);
DECLARE @TCNT INT = 0;
DECLARE @SPOS INT = 1; -- curr position in @S
DECLARE @JPOS INT = 1; -- curr position in @J
DECLARE @FCHR CHAR(1); -- char to... |
http://rosettacode.org/wiki/Jaro-Winkler_distance | Jaro-Winkler distance | The Jaro-Winkler distance is a metric for measuring the edit distance between words.
It is similar to the more basic Levenstein distance but the Jaro distance also accounts
for transpositions between letters in the words. With the Winkler modification to the Jaro
metric, the Jaro-Winkler distance also adds an increase ... | #Raku | Raku | sub jaro-winkler ($s, $t) {
return 0 if $s eq $t;
my $s_len = + my @s = $s.comb;
my $t_len = + my @t = $t.comb;
my $match_distance = ($s_len max $t_len) div 2 - 1;
my @s_matches;
my @t_matches;
my $matches = 0;
for ^@s -> $i {
my $start = 0 max $i - $match_distance;
... |
http://rosettacode.org/wiki/Inverted_syntax | Inverted syntax | Inverted syntax with conditional expressions
In traditional syntax conditional expressions are usually shown before the action within a statement or code block:
IF raining=true THEN needumbrella=true
In inverted syntax, the action is listed before the conditional expression in the statement or code block:
needumb... | #6502_Assembly | 6502 Assembly | loop_MySubroutine:
; more than 127 bytes of code
dex
bne loop_MySubroutine ;assembler will display an error message that the branch is too far away.
; rest of program |
http://rosettacode.org/wiki/Jaro_similarity | Jaro similarity | The Jaro distance is a measure of edit distance between two strings; its inverse, called the Jaro similarity, is a measure of two strings' similarity: the higher the value, the more similar the strings are. The score is normalized such that 0 equates to no similarities and 1 is an exact match.
Definition
The... | #Ada | Ada | with Ada.Text_IO;
procedure Jaro_Distances is
type Jaro_Measure is new Float;
function Jaro_Distance (Left, Right : in String) return Jaro_Measure
is
Left_Matches : array (Left'Range) of Boolean := (others => False);
Right_Matches : array (Right'Range) of Boolean := (others => False);
... |
http://rosettacode.org/wiki/Josephus_problem | Josephus problem | Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n
−
1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
,
removing eve... | #ANSI_Standard_BASIC | ANSI Standard BASIC | 100 FUNCTION josephus (n, k, m)
110 ! Return m-th on the reversed kill list; m=0 is final survivor.
120 LET lm = m ! Local copy OF m
130 FOR a = m+1 TO n
140 LET lm = MOD(lm+k, a)
150 NEXT a
160 LET josephus = lm
170 END FUNCTION
180 LET n = 41
190 LET k=3
200 PRINT "n =";n, "k =";k,"final survivo... |
http://rosettacode.org/wiki/Knuth_shuffle | Knuth shuffle | The Knuth shuffle (a.k.a. the Fisher-Yates shuffle) is an algorithm for randomly shuffling the elements of an array.
Task
Implement the Knuth shuffle for an integer array (or, if possible, an array of any type).
Specification
Given an array items with indices ranging from 0 to last, the algorithm can be d... | #Vlang | Vlang | import rand
import rand.seed
fn shuffle(mut arr []int) {
for i := arr.len - 1; i >= 0; i-- {
j := rand.intn(i + 1)
arr[i], arr[j] = arr[j], arr[i]
}
println('After Shuffle: $arr')
}
fn main() {
seed_array := seed.time_seed_array(2)
rand.seed(seed_array)
mut arr := [6, 9, 1, 4... |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #Rascal | Rascal | public num Jenssen(int lo, int hi, num (int i) term){
temp = 0;
while (lo <= hi){
temp += term(lo);
lo += 1;}
return temp;
} |
http://rosettacode.org/wiki/Jensen%27s_Device | Jensen's Device | Jensen's Device
You are encouraged to solve this task according to the task description, using any language you may know.
This task is an exercise in call by name.
Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report.
The following pr... | #REXX | REXX | /*REXX program demonstrates Jensen's device (via call subroutine, and args by name). */
parse arg d . /*obtain optional argument from the CL.*/
if d=='' | d=="," then d= 100 /*Not specified? Then use the default.*/
numeric digits d ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.