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/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Ruby
Ruby
dict = File.readlines("unixdict.txt").collect(&:strip) i = 0 res = dict.collect(&:reverse).sort.select do |z| i += 1 while z > dict[i] and i < dict.length-1 z == dict[i] and z < z.reverse end puts "There are #{res.length} semordnilaps, of which the following are 5:" res.take(5).each {|z| puts "#{z} #{z.reverse}...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
SEDOL[Code_?(Function[v,StringFreeQ[v,{"A","E","I","O","U"}]])]:= Code<>ToString[10-Mod[ToExpression[Quiet[Flatten[Characters[Code] /.x_?LetterQ->(ToCharacterCode[x]-55)]]].{1,3,1,7,3,9},10]] Scan[Print[SEDOL[#]] &, {"710889","B0YBKJ","406566","B0YBLH","228276","B0YBKL","557910","B0YBKR","585284","B0YBKT","B00030","DUM...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#MUMPS
MUMPS
SEDOL NEW A,B SEDOL1 READ !,"Enter the first 6 digits of a SEDOL: ",A SET B=$$SEDOLCHK(A) WRITE !,$SELECT($LENGTH(B)=1:"Full SEDOL is "_A_B,1:B) GOTO SEDOL1 QUIT SEDOLCHK(STOCK) NEW WT,VAL,I,CHK,C,FLAG SET WT="1317391",VAL=0,FLAG=0 FOR I=1:1:6 SET C=$$TOUPPER($EXTRACT(STOCK,I)),VAL=VAL+($$SEDVAL(C)*$EXTRACT(WT...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Picat
Picat
go =>   println([f(I) : I in 1..22]), nl, check(1_000_000), nl.   % The formula f(N) = N + floor(1/2 + sqrt(N)).   check(Limit) => Squares = new_map([I*I=1:I in 1..sqrt(Limit)]), Check = [[I,T] : I in 1..Limit-1, T=f(I), Squares.has_key(T)], println(check=Check.len).
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#PicoLisp
PicoLisp
(de sqfun (N) (+ N (sqrt N T)) ) # 'sqrt' rounds when called with 'T'   (for I 22 (println I (sqfun I)) )   (for I 1000000 (let (N (sqfun I) R (sqrt N)) (when (= N (* R R)) (prinl N " is square") ) ) )
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Maxima
Maxima
/* illustrating some functions on sets; names are self-explanatory */   a: {1, 2, 3, 4}; {1, 2, 3, 4}   b: {2, 4, 6, 8}; {2, 4, 6, 8}   intersection(a, b); {2, 4}   union(a, b); {1, 2, 3, 4, 6, 8}   powerset(a); {{}, {1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 4}, {1, 3}, {1, 3, 4}, {1, 4}, {2}, {2, 3}, {2, 3, 4}, {2,...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#EDSAC_order_code
EDSAC order code
  [Sieve of Eratosthenes] [EDSAC program. Initial Orders 2]   [Memory usage: 56..87 library subroutine P6, for printing 88..222 main program 224..293 mask table: 35 long masks; each has 34 1's and a single 0 294..1023 array of bits for integers 2, 3, 4, ..., where bit is changed from 1 to 0 ...
http://rosettacode.org/wiki/Sierpinski_carpet
Sierpinski carpet
Task Produce a graphical or ASCII-art representation of a Sierpinski carpet of order   N. For example, the Sierpinski carpet of order   3   should look like this: ########################### # ## ## ## ## ## ## ## ## # ########################### ### ###### ###### ### # # # ## # # ## # # # ### ###### ...
#zkl
zkl
fcn drawPat(x0,y0,s,img){ // Draw 3x3 pattern with hole in middle foreach y,x in (3,3){ if(x.isEven or y.isEven){ // don't draw middle pattern if(s>1) self.fcn(x*s + x0, y*s + y0, s/3, img); // recurse else img[x + x0, y + y0]=0xff0000; // red } } }
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Rust
Rust
use std::collections::HashSet; use std::fs::File; use std::io::{self, BufRead}; use std::iter::FromIterator;   fn semordnilap(filename: &str) -> std::io::Result<()> { let file = File::open(filename)?; let mut seen = HashSet::new(); let mut count = 0; for line in io::BufReader::new(file).lines() { ...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Scala
Scala
val wordsAll = scala.io.Source. fromURL("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"). getLines().map(_.toLowerCase).toIndexedSeq     /** * Given a sequence of lower-case words return a sub-sequence * of matches containing the word and its reverse if the two * words are different. */ def semordn...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Nim
Nim
proc c2v(c: char): int = assert c notin "AEIOU" if c < 'A': ord(c) - ord('0') else: ord(c) - ord('7')   const weight = [1, 3, 1, 7, 3, 9]   proc checksum(sedol: string): char = var val = 0 for i, ch in sedol: val += c2v(ch) * weight[i] result = chr((10 - val mod 10) mod 10 + ord('0'))   for sedol in ["710...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#OCaml
OCaml
let char2value c = assert (not (String.contains "AEIOU" c)); match c with | '0'..'9' -> int_of_char c - int_of_char '0' | 'A'..'Z' -> int_of_char c - int_of_char 'A' + 10 | _ -> assert false   let sedolweight = [1;3;1;7;3;9]   let explode s = s |> String.to_seq |> List.of_seq   let checksum sedol = let tm...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#PL.2FI
PL/I
  put skip edit ((n, n + floor(sqrt(n) + 0.5) do n = 1 to n)) (skip, 2 f(5));  
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#PostScript
PostScript
/nonsquare { dup sqrt .5 add floor add } def /issquare { dup sqrt floor dup mul eq } def   1 1 22 { nonsquare = } for   1 1 1000 { dup nonsquare issquare { (produced a square!) = = exit } if pop } for  
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Nanoquery
Nanoquery
class set declare internal_list   def set() internal_list = list() end def set(list) internal_list = list end   def append(value) if not value in internal_list internal_list.append(value) ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Eiffel
Eiffel
class APPLICATION   create make   feature make -- Run application. do across primes_through (100) as ic loop print (ic.item.out + " ") end end   primes_through (a_limit: INTEGER): LINKED_LIST [INTEGER] -- Prime numbers through `a_limit' requ...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Seed7
Seed7
$ include "seed7_05.s7i"; include "gethttp.s7i";   const func string: reverse (in string: word) is func result var string: drow is ""; local var integer: index is 0; begin for index range length(word) downto 1 do drow &:= word[index]; end for; end func;   const proc: main is func local...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Sidef
Sidef
var c = 0 var seen = Hash()   ARGF.each { |line| line.chomp! var r = line.reverse ((seen{r} := 0 ++) && (c++ < 5) && say "#{line} #{r}") -> || (seen{line} := 0 ++) }   say c
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Oforth
Oforth
func: sedol(s) [ 1, 3, 1, 7, 3, 9 ] s zipWith(#[ dup isDigit ifTrue: [ '0' - ] else: [ 'A' - 10 + ] * ]) sum 10 mod 10 swap - 10 mod StringBuffer new s << swap '0' + <<c ;
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Pascal
Pascal
program Sedols(output);   function index(c: char): integer; const alpha = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var i: integer; begin index := 0; for i := low(alpha) to high(alpha) do if c = alpha[i] then index := i; end;   function checkdigit(c: string): char; const weight...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#PowerShell
PowerShell
filter Get-NonSquare { return $_ + [Math]::Floor(1/2 + [Math]::Sqrt($_)) }
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#PureBasic
PureBasic
OpenConsole() For a = 1 To 22 ; Integer, so no floor needed tmp = 1 / 2 + Sqr(a) Print(Str(a + tmp) + ", ") Next PrintN("") PrintN("Starting check till one million") For a = 1 To 1000000 value.d = a + Round((1 / 2 + Sqr(a)), #PB_Round_Down) root = Sqr(value) If value - root*root = 0 found + 1 If ...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Nemerle
Nemerle
using System.Console; using Nemerle.Collections;   module RCSet { HasSubset[T](this super : Set[T], sub : Set[T]) : bool { super.ForAll(x => sub.Contains(x)) }   Main() : void { def names1 = Set(["Bob", "Billy", "Tom", "Dick", "Harry"]); def names2 = Set(["Bob", "Mary", "Alic...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Elixir
Elixir
defmodule Prime do def eratosthenes(limit \\ 1000) do sieve = [false, false | Enum.to_list(2..limit)] |> List.to_tuple check_list = [2 | Stream.iterate(3, &(&1+2)) |> Enum.take(round(:math.sqrt(limit)/2))] Enum.reduce(check_list, sieve, fn i,tuple -> if elem(tuple,i) do clear_num = Stream.it...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#SNOBOL4
SNOBOL4
  * Program: semordnilap.sbl * To run: sbl semordnilap.sbl < unixdict.txt * Description: A semordnilap is a word (or phrase) that spells * a different word (or phrase) backward. "Semordnilap" * is a word that itself is a semordnilap. * Example: lager and regal * Reads...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Stata
Stata
set seed 17760704 import delimited http://www.puzzlers.org/pub/wordlists/unixdict.txt, clear save temp, replace replace v1=strreverse(v1) merge 1:1 v1 using temp, nogen keep(3) drop if v1>=strreverse(v1) count 158 sample 5, count gen v2=strreverse(v1) list, noheader noobs +-------------+ | evil live | | pat ...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Perl
Perl
use List::Util qw(sum); use POSIX qw(strtol);   sub zip(&\@\@) { my $f = shift; my @a = @{shift()}; my @b = @{shift()}; my @result; push(@result, $f->(shift @a, shift @b)) while @a && @b; return @result; }   my @weights = (1, 3, 1, 7, 3, 9); sub sedol($) { my $s = shift; $s =~ /[AEIOU]/ and die "No vowe...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Python
Python
>>> from math import floor, sqrt >>> def non_square(n): return n + floor(1/2 + sqrt(n))   >>> # first 22 values has no squares: >>> print(*map(non_square, range(1, 23))) 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27   >>> # The following check shows no squares up to one million: >>> def is_square(...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Nim
Nim
var # creation s = {0, 3, 5, 10} t = {3..20, 50..55}   if 5 in s: echo "5 is in!" # element test   var c = s + t # union d = s * t # intersection e = s - t # difference   if s <= t: echo "s ⊆ t" # subset   if s < t: echo "s ⊂ t" # strong subset   if s == t: echo "s = s" # equality   s.incl(4) # add 4 to set ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Elm
Elm
module Main exposing (main)   import Browser exposing (element) import Task exposing (Task, succeed, perform, andThen) import Html exposing (Html, div, text) import Time exposing (now, posixToMillis)   type CIS a = CIS a (() -> CIS a)   uptoCIS2List : comparable -> CIS comparable -> List comparable uptoCIS2List n cis =...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#SuperCollider
SuperCollider
( var text, words, sdrow, semordnilap, selection; File.use("unixdict.txt".resolveRelative, "r", { |f| x = text = f.readAllString }); words = text.split(Char.nl).collect { |each| each.asSymbol }; sdrow = text.reverse.split(Char.nl).collect { |each| each.asSymbol }; semordnilap = sect(words, sdrow); // converted to symbo...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Phix
Phix
type string6(object s) return string(s) and length(s)=6 end type type sedolch(integer ch) return ch>='0' and ch<='Z' and (ch<='9' or ch>='A') and not find(ch,"AEIOU") end type function sedol(string6 t) sedolch c integer s = 0 for i=1 to 6 do c = t[i] s += iff(c>='A'?c-'A'+10:c-'0')*{1,3,...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Quackery
Quackery
$ "bigrat.qky" loadfile   [ dup n->v 2 vsqrt drop 1 2 v+ / + ] is nonsquare ( n --> n )   [ sqrt nip 0 = ] is squarenum ( n --> b )   say "Non-squares: " 22 times [ i^ 1+ nonsquare echo sp ] cr cr 0 999999 times [ i^ 1+ nonsquare squarenum if 1+ ] echo say " square numbers found"
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#R
R
nonsqr <- function(n) n + floor(1/2 + sqrt(n)) nonsqr(1:22)
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Objective-C
Objective-C
#import <Foundation/Foundation.h>   int main (int argc, const char *argv[]) { @autoreleasepool {   NSSet *s1 = [NSSet setWithObjects:@"a", @"b", @"c", @"d", @"e", nil]; NSSet *s2 = [NSSet setWithObjects:@"b", @"c", @"d", @"e", @"f", @"h", nil]; NSSet *s3 = [NSSet setWithObjects:@"b", @"c", @"d", nil]; ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Emacs_Lisp
Emacs Lisp
(defun sieve-set (limit) (let ((xs (make-vector (1+ limit) 0))) (cl-loop for i from 2 to limit when (zerop (aref xs i)) collect i and do (cl-loop for m from (* i i) to limit by i do (aset xs m 1)))))
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Swift
Swift
guard let data = try? String(contentsOfFile: "unixdict.txt") else { fatalError() }   let words = Set(data.components(separatedBy: "\n")) let pairs = words .map({ ($0, String($0.reversed())) }) .filter({ $0.0 < $0.1 && words.contains($0.1) })   print("Found \(pairs.count) pairs") print("Five examples: \(pairs.pref...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#PHP
PHP
function char2value($c) { assert(stripos('AEIOU', $c) === FALSE); return intval($c, 36); }   $sedolweight = array(1,3,1,7,3,9);   function checksum($sedol) { global $sedolweight; $tmp = array_sum(array_map(create_function('$ch, $weight', 'return char2value($ch) * $weight;'), s...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Racket
Racket
  #lang racket   (define (non-square n) (+ n (exact-floor (+ 1/2 (sqrt n)))))   (map non-square (range 1 23))   (define (square? n) (integer? (sqrt n)))   (for/or ([n (in-range 1 1000001)]) (square? (non-square n)))  
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Raku
Raku
sub nth-term (Int $n) { $n + round sqrt $n }   # Print the first 22 values of the sequence say (nth-term $_ for 1 .. 22);   # Check that the first million values of the sequence are indeed non-square for 1 .. 1_000_000 -> $i { say "Oops, nth-term($i) is square!" if (sqrt nth-term $i) %% 1; }
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#OCaml
OCaml
# module IntSet = Set.Make(struct type t = int let compare = compare end);; (* Create a module for our type of set *) module IntSet : sig type elt = int type t val empty : t val is_empty : t -> bool val mem : elt -> t -> bool val add : elt -> t -> t val singleton : elt -> t val remove ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Erlang
Erlang
  -module( sieve_of_eratosthenes ).   -export( [primes_upto/1] ).   primes_upto( N ) -> Ns = lists:seq( 2, N ), Dict = dict:from_list( [{X, potential_prime} || X <- Ns] ), {Upto_sqrt_ns, _T} = lists:split( erlang:round(math:sqrt(N)), Ns ), {N, Prime_dict} = lists:foldl( fun find_prime/2, {N, Dict}, Upto_sqrt_ns ), ...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Tcl
Tcl
package require Tcl 8.5 package require http   # Fetch the words set t [http::geturl http://www.puzzlers.org/pub/wordlists/unixdict.txt] set wordlist [split [http::data $t] \n] http::cleanup $t   # Build hash table for speed foreach word $wordlist { set reversed([string reverse $word]) "dummy" }   # Find where a re...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#PicoLisp
PicoLisp
(de sedol (Str) (pack Str (char (+ `(char "0") (% (- 10 (% (sum '((W C) (cond ((>= "9" C "0") (* W (format C)) ) ...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Red
Red
Red ["Sequence of non-squares"]   repeat i 999'999 [ n: i + round/floor 0.5 + sqrt i if i < 23 [prin [to-integer n ""]] if equal? round/floor n sqrt n [ print "Square found!" break ] ]
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#REXX
REXX
/*REXX pgm displays some non─square numbers, & also displays a validation check up to 1M*/ parse arg N M . /*obtain optional arguments from the CL*/ if N=='' | N=="," then N= 22 /*Not specified? Then use the default.*/ if M=='' | M=="," then M= 1000000 ...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Ol
Ol
  ; test set (define set1 '(1 2 3 4 5 6 7 8 9)) (define set2 '(3 4 5 11 12 13 14)) (define set3 '(4 5 6 7)) (define set4 '(1 2 3 4 5 6 7 8 9))   ; union (print (union set1 set2)) ; ==> (1 2 6 7 8 9 3 4 5 11 12 13 14)   ; intersection (print (intersect set1 set2)) ; ==> (3 4 5)   ; difference (print (diff set1 set2)) ; ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#ERRE
ERRE
  PROGRAM SIEVE_ORG  ! --------------------------------------------------  ! Eratosthenes Sieve Prime Number Program in BASIC  ! (da 3 a SIZE*2) from Byte September 1981  !--------------------------------------------------- CONST SIZE%=8190   DIM FLAGS%[SIZE%]   BEGIN PRINT("Only 1 iteration") COUNT%=0 ...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Transd
Transd
#lang transd   MainModule: { _start: (λ (with fs FileStream() (open-r fs "/mnt/vault/tmp/unixdict.txt") ) (with v ( -| (read-text fs) (split) (group-by (λ s String() -> String() (ret (min s (reverse (cp s)))))) (values) (filter where: (λ v Vector<String>() (...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#TUSCRIPT
TUSCRIPT
  $$ MODE TUSCRIPT,{} requestdata = REQUEST ("http://www.puzzlers.org/pub/wordlists/unixdict.txt") DICT semordnilap CREATE 99999 COMPILE LOOP r=requestdata rstrings=STRINGS(r," ? ") rreverse=REVERSE(rstrings) revstring=EXCHANGE (rreverse,":'':':'::") group=APPEND (r,revstring) sort=ALPHA_SORT (group) DICT semordnilap A...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#PL.2FI
PL/I
/* Compute SEDOLs; includes check for invalid characters. */ sedol: procedure options (main); /* 3 March 2012 */ declare alphabet character (36) static initial ('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'); declare weight (6) fixed static initial (1, 3, 1, 7, 3, 9); declare s character (6); declare (i, v, ...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Ring
Ring
  for n=1 to 22 x = n + floor(1/2 + sqrt(n)) see "" + x + " " next see nl  
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Ruby
Ruby
def f(n) n + (0.5 + Math.sqrt(n)).floor end   (1..22).each { |n| puts "#{n} #{f(n)}" }   non_squares = (1..1_000_000).map { |n| f(n) } squares = (1..1001).map { |n| n**2 } # Note: 1001*1001 = 1_002_001 > 1_001_000 = f(1_000_000) (squares & non_squares).each do |n| puts "Oops, found a square f(#{non_squares.index(n)...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#ooRexx
ooRexx
-- Set creation -- Using the OF method s1 = .set~of(1, 2, 3, 4, 5, 6) -- Explicit addition of individual items s2 = .set~new s2~put(2) s2~put(4) s2~put(6) -- group addition s3 = .set~new s3~putall(.array~of(1, 3, 5)) -- Test m ? S -- "m is an element in set S" say s1~hasindex(1) s3~hasindex(2) -- "1 0", which is "true...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Euphoria
Euphoria
constant limit = 1000 sequence flags,primes flags = repeat(1, limit) for i = 2 to sqrt(limit) do if flags[i] then for k = i*i to limit by i do flags[k] = 0 end for end if end for   primes = {} for i = 2 to limit do if flags[i] = 1 then primes &= i end if end for ? pri...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#VBScript
VBScript
  Set objFSO = CreateObject("Scripting.FileSystemObject") Set objInFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_ "\unixdict.txt",1)   Set objUnixDict = CreateObject("Scripting.Dictionary") Set objSemordnilap = CreateObject("Scripting.Dictionary")   Do Until objInFile.AtEndOfStream ...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#Wren
Wren
import "io" for File   var dict = File.read("unixdict.txt").split("\n") var wmap = {} dict.each { |w| wmap[w] = true } var pairs = [] var used = {} for (word in dict) { if (word != "") { var pal = word[-1..0] if (word != pal && wmap[pal] && !used[pal]) { pairs.add([word, pal]) ...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Potion
Potion
sedolnum = (c) : if ("0" ord <= c ord and c ord <= "9" ord): c number integer. else: 10 + c ord - "A" ord. .   sedol = (str) : weight = (1, 3, 1, 7, 3, 9) sum = 0 6 times (i) : sum = sum + sedolnum(str(i)) * weight(i) . (str, (10 - (sum % 10)) % 10) join .
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#PowerShell
PowerShell
function Add-SEDOLCheckDigit { Param ( # Validate input as six-digit SEDOL number [ValidatePattern( "^[0123456789bcdfghjklmnpqrstvwxyz]{6}$" )] [parameter ( Mandatory = $True ) ] [string] $SixDigitSEDOL )   # Convert to array of single character strings, usi...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Rust
Rust
  fn f(n: i64) -> i64 { n + (0.5 + (n as f64).sqrt()) as i64 }   fn is_sqr(n: i64) -> bool { let a = (n as f64).sqrt() as i64; n == a * a || n == (a+1) * (a+1) || n == (a-1) * (a-1) }   fn main() { println!( "{:?}", (1..23).map(|n| f(n)).collect::<Vec<i64>>() ); let count = (1..1_000_000).map(|n| f...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Scala
Scala
def nonsqr(n:Int)=n+math.round(math.sqrt(n)).toInt   for(n<-1 to 22) println(n + " "+ nonsqr(n))   val test=(1 to 1000000).exists{n => val j=math.sqrt(nonsqr(n)) j==math.floor(j) } println("squares up to one million="+test)
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#PARI.2FGP
PARI/GP
setsubset(s,t)={ for(i=1,#s, if(!setsearch(t,s[i]), return(0)) ); 1 }; s=Set([1,2,2]) t=Set([4,2,4]) setsearch(s,1) setunion(s,t) setintersect(s,t) setminus(s,t) setsubset(s,t) s==t
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#F.23
F#
  let primes max = let mutable xs = [|2..max|] let limit = max |> float |> sqrt |> int for x in [|2..limit|] do xs <- xs |> Array.except [|x*x..x..max|] xs  
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#XPL0
XPL0
include c:\cxpl\codes; \intrinsic 'code' declarations string 0; \use zero-terminated strings def LF=$0A, CR=$0D, EOF=$1A;   proc RevStr(S); \Reverse order of characters in a string char S; int I, J, T; [J:= 0; while S(J) do J:= J+1; J:= J-1; I:= 0; while I<J do [T:= S(I); S(I):= S(J); ...
http://rosettacode.org/wiki/Semordnilap
Semordnilap
A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal Task This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap ...
#zkl
zkl
var [const] words= // create hashed unixdict of striped words (word:True, ...) File("dict.txt").howza(11).pump(Dictionary().howza(8).add.fp1(True)); ss:=words.pump(List, // push stripped unixdict words through some functions fcn(w){ words.holds(w.reverse()) }, Void.Filter, // filter palindromes // create ("...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#PureBasic
PureBasic
Procedure.s SEDOLs(rawstring$) Protected i, j, sum, c, m For i=1 To Len(rawstring$) c=Asc(Mid(rawstring$,i,1)) Select c Case Asc("0") To Asc("9") j=Val(Mid(rawstring$,i,1)) Default j=c-Asc("A") EndSelect Select i Case 1, 3, 7: m=1 Case 2, 5: m=3 Cas...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Scheme
Scheme
(define non-squares (lambda (index) (+ index (inexact->exact (floor (+ (/ 1 2) (sqrt index)))))))   (define sequence (lambda (function) (lambda (start) (lambda (stop) (if (> start stop) (list) (cons (function start) (((sequence function) (+ start 1)) s...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Pascal
Pascal
program Rosetta_Set;   {$mode objfpc}{$H+}   uses {$IFDEF UNIX} {$IFDEF UseCThreads} cthreads, {$ENDIF} {$ENDIF} Classes;   {$R *.res} type CharSet = set of char;   var A, B, C, S: CharSet; M: char;   function SetToString(const ASet: CharSet): string; var J: char; begin Result := ''; // Test...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Factor
Factor
USING: bit-arrays io kernel locals math math.functions math.ranges prettyprint sequences ; IN: rosetta-code.sieve-of-erato   <PRIVATE   : init-sieve ( n -- seq )  ! Include 0 and 1 for easy indexing. 1 - <bit-array> dup set-bits ?{ f f } prepend ;   ! Given the sieve and a prime starting index, create a range of !...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Python
Python
def char2value(c): assert c not in 'AEIOU', "No vowels" return int(c, 36)   sedolweight = [1,3,1,7,3,9]   def checksum(sedol): tmp = sum(map(lambda ch, weight: char2value(ch) * weight, sedol, sedolweight) ) return str((10 - (tmp % 10)) % 10)   for sedol in ''' 710889 ...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Seed7
Seed7
$ include "seed7_05.s7i"; include "float.s7i"; include "math.s7i";   const func integer: nonsqr (in integer: n) is return n + trunc(0.5 + sqrt(flt(n)));   const proc: main is func local var integer: i is 0; var float: j is 0.0; begin # First 22 values (as a list) has no squares: for i range 1 ...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Sidef
Sidef
func nonsqr(n) { 0.5 + n.sqrt -> floor + n } {|i| nonsqr(i) }.map(1..22).join(' ').say   { |i| if (nonsqr(i).is_sqr) { die "Found a square in the sequence: #{i}" } } << 1..1e6
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Perl
Perl
use strict;   package Set; # likely will conflict with stuff on CPAN use overload '""' => \&str, 'bool' => \&count, '+=' => \&add, '-=' => \&del, '-' => \&diff, '==' => \&eq, '&' => \&intersection, '|' => \&union, '^' => \&xdiff;   sub str { my $set = shift; # This has drawbacks: stringification is used as s...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#FOCAL
FOCAL
1.1 T "PLEASE ENTER LIMIT" 1.2 A N 1.3 I (2047-N)5.1 1.4 D 2 1.5 Q   2.1 F X=2,FSQT(N); D 3 2.2 F W=2,N; I (SIEVE(W)-2)4.1   3.1 I (-SIEVE(X))3.3 3.2 F Y=X*X,X,N; S SIEVE(Y)=2 3.3 R   4.1 T %4.0,W,!   5.1 T "PLEASE ENTER A NUMBER LESS THAN 2048."!; G 1.1
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Q
Q
scd:{ v:{("i"$x) - ?[("0"<=x) & x<="9"; "i"$"0"; -10+"i"$"A"]} each x; / Turn characters of SEDOL into their values w:sum v*1 3 1 7 3 9; / Weighted sum of values d:(10 - w mod 10) mod 10; / Check digit value x,"c"$(("i"$"0")+d) / Append to SEDOL }   scd each ("710889";"B0YBKJ";"406566";...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Smalltalk
Smalltalk
| nonSquare isSquare squaresFound | nonSquare := [:n | n + (n sqrt) rounded ]. isSquare := [:n | n = (((n sqrt) asInteger) raisedTo: 2) ]. Transcript show: 'The first few non-squares:'; cr. 1 to: 22 do: [:n | Transcript show: (nonSquare value: n) asString; cr ]. squaresFound := 0. 1 to: 1000000 do: [:n | ...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Standard_ML
Standard ML
- fun nonsqr n = n + round (Math.sqrt (real n)); val nonsqr = fn : int -> int - List.tabulate (23, nonsqr); val it = [0,2,3,5,6,7,8,10,11,12,13,14,...] : int list - let fun loop i = if i = 1000000 then true else let val j = Math.sqrt (real (nonsqr i)) in ...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Phix
Phix
with javascript_semantics function is_element(object x, sequence set) return find(x,set)!=0 end function function set_union(sequence set1, set2) for i=1 to length(set2) do if not is_element(set2[i],set1) then set1 = append(set1,set2[i]) end if end for return set1 end functi...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Forth
Forth
: prime? ( n -- ? ) here + c@ 0= ; : composite! ( n -- ) here + 1 swap c! ; : sieve ( n -- ) here over erase 2 begin 2dup dup * > while dup prime? if 2dup dup * do i composite! dup +loop then 1+ repeat drop ." Primes: " 2 do i prime? if i . then loop ; 100 sieve
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#R
R
# Read in data from text connection datalines <- readLines(tc <- textConnection("710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT")); close(tc)   # Check data valid checkSedol <- function(datalines) { ok <- grep("^[[:digit:][:upper:]]{6}$", datalines) if(length(ok) < length(datalines)) { ...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Tcl
Tcl
package require Tcl 8.5   set f {n {expr {$n + floor(0.5 + sqrt($n))}}}   for {set x 1} {$x <= 22} {incr x} { puts [format "%d\t%s" $x [apply $f $x]] }   puts "looking for a square..." for {set x 1} {$x <= 1000000} {incr x} { set y [apply $f $x] set s [expr {sqrt($y)}] if {$s == int($s)} { error...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#TI-89_BASIC
TI-89 BASIC
■ n+floor(1/2+√(n)) → f(n) Done ■ seq(f(n),n,1,22) {2,3,5,6,7,8,10,11,12,13,14,15,17,18,19,20,21,22,23,24,26,27}
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#PicoLisp
PicoLisp
(setq Set1 (1 2 3 7 abc "def" (u v w)) Set2 (2 3 5 hello (x y z)) Set3 (3 hello (x y z)) )     # Element tests (any non-NIL value means "yes") : (member "def" Set1) -> ("def" (u v w))   : (member "def" Set2) -> NIL   : (member '(x y z) Set2) -> ((x y z))     # Union : (uniq (append Set1 Set2)) -> (1 2 3 7 abc ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Fortran
Fortran
program sieve   implicit none integer, parameter :: i_max = 100 integer :: i logical, dimension (i_max) :: is_prime   is_prime = .true. is_prime (1) = .false. do i = 2, int (sqrt (real (i_max))) if (is_prime (i)) is_prime (i * i : i_max : i) = .false. end do do i = 1, i_max if (is_prime (i)) w...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Racket
Racket
#lang racket ;;; Since the Task gives us unchecksummed and checksummed SEDOLs, and ;;; we'll just take a list of the output SEDOLs and remove their last ;;; characters for the input (define output-SEDOLS (list "7108899" "B0YBKJ7" "4065663" "B0YBLH2" "2282765" "B0YBKL9" "5579107" "B0YBKR5" "5852842" ...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Raku
Raku
sub sedol( Str $s ) { die 'No vowels allowed' if $s ~~ /<[AEIOU]>/; die 'Invalid format' if $s !~~ /^ <[0..9B..DF..HJ..NP..TV..Z]>**6 $ /;   my %base36 = (flat 0..9, 'A'..'Z') »=>« ^36; my @weights = 1, 3, 1, 7, 3, 9;   my @vs = %base36{ $s.comb }; my $checksum = [+] @vs Z* @weights; my ...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Transd
Transd
#lang transd   MainModule: { nonsqr: (λ i Int() (ret (+ i (to-Int (floor (+ 0.5 (sqrt i))))))),   _start: (lambda locals: d Double() (for i in Range(1 23) do (textout (nonsqr i) " "))   (for i in Range(1 1000001) do (= d (sqrt (nonsqr i))) (if (eq d (floo...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#True_BASIC
True BASIC
FUNCTION nonSquare (n) LET nonSquare = n + INT(0.5 + SQR(n)) END FUNCTION   ! Display first 22 values PRINT "The first 22 numbers generated by the sequence are : " FOR i = 1 TO 22 PRINT nonSquare(i); " "; NEXT i PRINT   ! Check FOR squares up TO one million LET found = 0 FOR i = 1 TO 1e6 LET j = SQR(nonSqua...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#PowerShell
PowerShell
  [System.Collections.Generic.HashSet[object]]$set1 = 1..4 [System.Collections.Generic.HashSet[object]]$set2 = 3..6   # Operation + Definition + Result #--------------------------------+---------------------+------------------------- $set1.UnionWith($set2) # Union ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Free_Pascal
Free Pascal
  program prime_sieve; {$mode objfpc}{$coperators on} uses SysUtils, GVector; type TPrimeList = specialize TVector<DWord>; function Sieve(aLimit: DWord): TPrimeList; var IsPrime: array of Boolean; I, SqrtBound: DWord; J: QWord; begin Result := TPrimeList.Create; Inc(aLimit, Ord(aLimit < High(DWord))); //n...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#REXX
REXX
╔════════════════════════════════════════════════════════════════════╗ ║ If the SEDOL is 6 characters, a check digit is added. ║ ║ ║ ║ If the SEDOL is 7 characters, a check digit is created and it's ║ ...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Ring
Ring
  see sedol("710889") + nl see sedol("B0YBKJ") + nl see sedol("406566") + nl see sedol("B0YBLH") + nl see sedol("228276") + nl see sedol("B0YBKL") + nl see sedol("557910") + nl see sedol("B0YBKR") + nl see sedol("585284") + nl see sedol("B0YBKT") + nl see sedol("B00030") + nl   func sedol d d = upper(d) s = 0...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Ursala
Ursala
#import nat #import flo   nth_non_square = float; plus^/~& math..trunc+ plus/0.5+ sqrt is_square = sqrt; ^E/~& math..trunc   #show+   examples = %neALP ^(~&,nth_non_square)*t iota23 check = (is_square*~+nth_non_square*t; ~&i&& %eLP)||-[no squares found]-! iota 1000000
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#VBA
VBA
  Sub Main() Dim i&, c&, j#, s$ Const N& = 1000000 s = "values for n in the range 1 to 22 : " For i = 1 To 22 s = s & ns(i) & ", " Next For i = 1 To N j = Sqr(ns(i)) If j = CInt(j) Then c = c + 1 Next   Debug.Print s Debug.Print c & " squares less than " & N End Sub   Private Func...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#Prolog
Prolog
:- use_module(library(lists)).   set :- A = [2, 4, 1, 3], B = [5, 2, 3, 2], ( is_set(A) -> format('~w is a set~n', [A]) ; format('~w is not a set~n', [A])), ( is_set(B) -> format('~w is a set~n', [B]) ; format('~w is not a set~n', [B])),   % create a set from a list   list_to_set(B, BS), ( is_set(BS)...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#FreeBASIC
FreeBASIC
' FB 1.05.0   Sub sieve(n As Integer) If n < 2 Then Return Dim a(2 To n) As Integer For i As Integer = 2 To n : a(i) = i : Next Dim As Integer p = 2, q ' mark non-prime numbers by setting the corresponding array element to 0 Do For j As Integer = p * p To n Step p a(j) = 0 Next j ' look fo...
http://rosettacode.org/wiki/SEDOLs
SEDOLs
Task For each number list of 6-digit SEDOLs, calculate and append the checksum digit. That is, given this input: 710889 B0YBKJ 406566 B0YBLH 228276 B0YBKL 557910 B0YBKR 585284 B0YBKT B00030 Produce this output: 7108899 B0YBKJ7 4065663 B0YBLH2 2282765 B0YBKL9 5579107 B0YBKR5 5852842 B0YBKT7 B000300 Extra credit ...
#Ruby
Ruby
Sedol_char = "0123456789BCDFGHJKLMNPQRSTVWXYZ" Sedolweight = [1,3,1,7,3,9]   def char2value(c) raise ArgumentError, "Invalid char #{c}" unless Sedol_char.include?(c) c.to_i(36) end   def checksum(sedol) raise ArgumentError, "Invalid length" unless sedol.size == Sedolweight.size sum = sedol.chars.zip(Sedolweight...
http://rosettacode.org/wiki/Sequence_of_non-squares
Sequence of non-squares
Task Show that the following remarkable formula gives the sequence of non-square natural numbers: n + floor(1/2 + sqrt(n)) Print out the values for   n   in the range   1   to   22 Show that no squares occur for   n   less than one million This is sequence   A000037   in the OEIS database.
#Wren
Wren
import "/fmt" for Fmt   System.print("The first 22 numbers in the sequence are:") System.print(" n term") for (n in 1...1e6) { var s = n + (0.5 + n.sqrt).floor var ss = s.sqrt.round if (ss * ss == s) { Fmt.print("The $r number in the sequence $d = $d x $d is a square.", n, s, ss, ss) retur...
http://rosettacode.org/wiki/Set
Set
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A   set  is a collection of elements, without duplicates and without order. Task Show each of these set operations: Set creation Test m ∈ S -- "m is an...
#PureBasic
PureBasic
Procedure.s booleanText(b) ;returns 'True' or 'False' for a boolean input If b: ProcedureReturn "True": EndIf ProcedureReturn "False" EndProcedure   Procedure.s listSetElements(Map a(), delimeter.s = " ") ;format elements for display Protected output$   ForEach a() output$ + MapKey(a()) + delimeter Next  ...
http://rosettacode.org/wiki/Sieve_of_Eratosthenes
Sieve of Eratosthenes
This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task. The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer. Task Implement the   Sieve of Eratosthenes   algorithm, with the only allowed...
#Frink
Frink
  n = eval[input["Enter highest number: "]] results = array[sieve[n]] println[results] println[length[results] + " prime numbers less than or equal to " + n]   sieve[n] := { // Initialize array array = array[0 to n] array@1 = 0   for i = 2 to ceil[sqrt[n]] if array@i != 0 for j = i^2 to n ste...