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/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #ALGOL_68 | ALGOL 68 | BEGIN
# find solutions to Pell's eqauation: x^2 - ny^2 = 1 for integer x, y, n #
MODE BIGINT = LONG LONG INT;
MODE BIGPAIR = STRUCT( BIGINT v1, v2 );
PROC solve pell = ( INT n )BIGPAIR:
IF INT x = ENTIER( sqrt( n ) );
x * x = n
THEN
# n is a erfect square... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #jq | jq | # Input: {svg, minx, miny, maxx, maxy}
def svg:
# viewBox = <min-x> <min-y> <width> <height>
"<svg viewBox='\(.minx - 4|floor) \(.miny - 4 |floor) \(6 + .maxx - .minx|ceil) \(6 + .maxy - .miny|ceil)'",
" preserveAspectRatio='xMinYmin meet'",
" xmlns='http://www.w3.org/2000/svg' >",
.svg,
"</svg>" ;
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Julia | Julia | using Luxor
function drawpentagram(path::AbstractString, w::Integer=1000, h::Integer=1000)
Drawing(h, w, path)
origin()
setline(16)
# To get a different color border from the fill, draw twice, first with fill, then without.
sethue("aqua")
star(0, 0, 500, 5, 0.39, 3pi/10, :fill)
sethue(... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #OpenEdge.2FProgress | OpenEdge/Progress |
DEFINE VARIABLE charArray AS CHARACTER EXTENT 3 INITIAL ["A","B","C"].
DEFINE VARIABLE sizeofArray AS INTEGER.
sizeOfArray = EXTENT(charArray).
RUN GetPermutations(1).
PROCEDURE GetPermutations:
DEFINE INPUT PARAMETER n AS INTEGER.
DEFINE VARIABLE i AS INTEGER.
DEFINE VARIABLE j AS I... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Seed7 | Seed7 | $ include "seed7_05.s7i";
const func boolean: isPerfect (in integer: n) is func
result
var boolean: isPerfect is FALSE;
local
var integer: i is 0;
var integer: sum is 1;
var integer: q is 0;
begin
for i range 2 to sqrt(n) do
if n rem i = 0 then
sum +:= i;
q := n div i;
... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Sidef | Sidef | func is_perfect(n) {
n.sigma == 2*n
}
for n in (1..10000) {
say n if is_perfect(n)
} |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Arturo | Arturo | solvePell: function [n][
x: to :integer sqrt n
[y, z, r]: @[x, 1, shl x 1]
[e1, e2]: [1, 0]
[f1, f2]: [0, 1]
while [true][
y: (r * z) - y
z: (n - y * y) / z
r: (x + y) / z
[e1, e2]: @[e2, e1 + e2 * r]
[f1, f2]: @[f2, f1 + f2 * r]
[a, b]: @[e2 + f2 ... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Kotlin | Kotlin | // version 1.1.2
import java.awt.*
import java.awt.geom.Path2D
import javax.swing.*
class Pentagram : JPanel() {
init {
preferredSize = Dimension(640, 640)
background = Color.white
}
private fun drawPentagram(g: Graphics2D, len: Int, x: Int, y: Int,
fill: ... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #PARI.2FGP | PARI/GP | vector(n!,k,numtoperm(n,k)) |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Simula | Simula | BOOLEAN PROCEDURE PERF(N); INTEGER N;
BEGIN
INTEGER SUM;
FOR I := 1 STEP 1 UNTIL N-1 DO
IF MOD(N, I) = 0 THEN
SUM := SUM + I;
PERF := SUM = N;
END PERF; |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Slate | Slate | n@(Integer traits) isPerfect
[
(((2 to: n // 2 + 1) select: [| :m | (n rem: m) isZero])
inject: 1 into: #+ `er) = n
]. |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #C | C | #include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
struct Pair {
uint64_t v1, v2;
};
struct Pair makePair(uint64_t a, uint64_t b) {
struct Pair r;
r.v1 = a;
r.v2 = b;
return r;
}
struct Pair solvePell(int n) {
int x = (int) sqrt(n);
if (x * x == n) {
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Lua | Lua | local cos, sin, floor, pi = math.cos, math.sin, math.floor, math.pi
function Bitmap:render()
for y = 1, self.height do
print(table.concat(self.pixels[y]))
end
end
function Bitmap:pentagram(x, y, radius, rotation, outlcolor, fillcolor)
local function pxy(i) return x+radius*cos(i*pi*2/5+rotation), y+radius*... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #Pascal | Pascal | program perm;
var
p: array[1 .. 12] of integer;
is_last: boolean;
n: integer;
procedure next;
var i, j, k, t: integer;
begin
is_last := true;
i := n - 1;
while i > 0 do
begin
if p[i] < p[i + 1] then
begin
is_last := false;
break;
end;
i := i - 1;
end;
if not is_last then
begin
j := i + 1;
k := n... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Smalltalk | Smalltalk | Integer extend [
"Translation of the C version; this is faster..."
isPerfectC [ |tot| tot := 1.
(2 to: (self sqrt) + 1) do: [ :i |
(self rem: i) = 0
ifTrue: [ |q|
tot := tot + i.
q := self // i.
q > i ifTrue: [ tot := tot + q ]
]
... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #C.2B.2B | C++ | #include <iomanip>
#include <iostream>
#include <tuple>
std::tuple<uint64_t, uint64_t> solvePell(int n) {
int x = (int)sqrt(n);
if (x * x == n) {
// n is a perfect square - no solution other than 1,0
return std::make_pair(1, 0);
}
// there are non-trivial solutions
int y = x;
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Maple | Maple | with(geometry):
RegularStarPolygon(middle, 5/2, point(c, 0, 0), 1):
v := [seq(coordinates(i), i in DefinedAs(middle))]:
pentagram := plottools[rotate](plottools[polygon](v), Pi/2):
plots[display](pentagram, colour = yellow, axes = none); |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Mathematica.2FWolfram_Language | Mathematica/Wolfram Language | Graphics[{
EdgeForm[Directive[Thickness[0.01], RGBColor[0, 0, 1]]],(*Edge coloring*)
RGBColor[0.5, 0.5, .50], (*Fill coloring*)
Polygon[AnglePath[Table[6 Pi/5, 5]]]}
] |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #Perl | Perl | sub permutation {
my ($perm,@set) = @_;
print "$perm\n" || return unless (@set);
permutation($perm.$set[$_],@set[0..$_-1],@set[$_+1..$#set]) foreach (0..$#set);
}
my @input = (qw/a b c d/);
permutation('',@input); |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Swift | Swift | func perfect(n:Int) -> Bool {
var sum = 0
for i in 1..<n {
if n % i == 0 {
sum += i
}
}
return sum == n
}
for i in 1..<10000 {
if perfect(i) {
println(i)
}
} |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Tcl | Tcl | proc perfect n {
set sum 0
for {set i 1} {$i <= $n} {incr i} {
if {$n % $i == 0} {incr sum $i}
}
expr {$sum == 2*$n}
} |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #C.23 | C# | using System;
using System.Numerics;
static class Program
{
static void Fun(ref BigInteger a, ref BigInteger b, int c)
{
BigInteger t = a; a = b; b = b * c + t;
}
static void SolvePell(int n, ref BigInteger a, ref BigInteger b)
{
int x = (int)Math.Sqrt(n), y = x, z = 1, r = x << ... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Nim | Nim |
import libgd
from math import sin, cos, degToRad
const
width = 500
height = width
outerRadius = 200
proc main() =
proc calcPosition(x: int, y: int, radius: int, posAngle: float): (cint, cint) =
var width = int(radius.float * sin(degToRad(posAngle)))
var height = int(radius.float * cos(degToRad(p... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #ooRexx | ooRexx | /* REXX ***************************************************************
* Create a BMP file showing a pentagram
**********************************************************************/
pentagram='pentagram.bmp'
'erase' pentagram
s='424d4600000000000000360000002800000038000000280000000100180000000000'X
s=s'10000000000000... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #Phix | Phix | with javascript_semantics
requires("1.0.2")
?shorten(permutes("abcd"),"elements",5)
|
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Ursala | Ursala | #import std
#import nat
is_perfect = ~&itB&& ^(~&,~&t+ iota); ^E/~&l sum:-0+ ~| not remainder |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #VBA | VBA | Private Function Factors(x As Long) As String
Application.Volatile
Dim i As Long
Dim cooresponding_factors As String
Factors = 1
corresponding_factors = x
For i = 2 To Sqr(x)
If x Mod i = 0 Then
Factors = Factors & ", " & i
If i <> x / i Then corresponding_factors... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #D | D | import std.bigint;
import std.math;
import std.stdio;
void fun(ref BigInt a, ref BigInt b, int c) {
auto t = a;
a = b;
b = b * c + t;
}
void solvePell(int n, ref BigInt a, ref BigInt b) {
int x = cast(int) sqrt(cast(real) n);
int y = x;
int z = 1;
int r = x << 1;
BigInt e1 = 1;
B... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Perl | Perl | use SVG;
my $tau = 2 * 4*atan2(1, 1);
my $dim = 200;
my $sides = 5;
for $v (0, 2, 4, 1, 3, 0) {
push @vx, 0.9 * $dim * cos($tau * $v / $sides);
push @vy, 0.9 * $dim * sin($tau * $v / $sides);
}
my $svg= SVG->new( width => 2*$dim, height => 2*$dim);
my $points = $svg->get_path(
x => \@vx,
... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #Phixmonti | Phixmonti | include ..\Utilitys.pmt
def save
over over chain ps> swap 0 put >ps
enddef
def permute /# l l -- #/
len 2 > if
len for drop
pop swap rot swap 1 put swap permute
endfor
else
save rotate save rotate
endif
swap len if
pop rot rot 0 put
else
d... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #VBScript | VBScript | Function IsPerfect(n)
IsPerfect = False
i = n - 1
sum = 0
Do While i > 0
If n Mod i = 0 Then
sum = sum + i
End If
i = i - 1
Loop
If sum = n Then
IsPerfect = True
End If
End Function
WScript.StdOut.Write IsPerfect(CInt(WScript.Arguments(0)))
WScript.StdOut.WriteLine |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #11l | 11l | V first = random:choice([1B, 0B])
V you = ‘’
V me = ‘’
V ht = ‘HT’ // to get round ‘bug in MSVC 2017’[https://developercommunity.visualstudio.com/t/bug-with-operator-in-c/565417]
I first
me = random:sample(Array(‘HT’ * 3), 3).join(‘’)
print(‘I choose first and will win on first seeing #. in the list of tosses’.... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #360_Assembly | 360 Assembly | * Pathological floating point problems 03/05/2016
PATHOFP CSECT
USING PATHOFP,R13
SAVEAR B STM-SAVEAR(R15)
DC 17F'0'
STM STM R14,R12,12(R13)
ST R13,4(R15)
ST R15,8(R13)
LR R13,R15
LE F0,=E'2'
STE F0,U ... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Delphi | Delphi |
program Pells_equation;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
Velthuis.BigIntegers;
type
TPellResult = record
x, y: BigInteger;
end;
function SolvePell(nn: UInt64): TPellResult;
var
n, x, y, z, r, e1, e2, f1, t, u, a, b: BigInteger;
begin
n := nn;
x := nn;
x := BigInteger.Sqrt(x);
y ... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Phix | Phix | --
-- demo\rosetta\Pentagram.exw
-- ==========================
--
-- Start/stop rotation by pressing space. Resizeable.
-- ZXYV stop any rotation and orient up/down/left/right.
--
with javascript_semantics
include pGUI.e
Ihandle dlg, canvas, timer
cdCanvas cdcanvas
integer rot = 0
enum FILL,BORDER
constant colours = ... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #Picat | Picat | permutation_rec1([X|Y],Z) :-
permutation_rec1(Y,W),
select(X,Z,W).
permutation_rec1([],[]).
permutation_rec2([], []).
permutation_rec2([X], [X]) :-!.
permutation_rec2([T|H], X) :-
permutation_rec2(H, H1),
append(L1, L2, H1),
append(L1, [T], X1),
append(X1, L2, X). |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Vlang | Vlang | fn compute_perfect(n i64) bool {
mut sum := i64(0)
for i := i64(1); i < n; i++ {
if n%i == 0 {
sum += i
}
}
return sum == n
}
// following fntion satisfies the task, returning true for all
// perfect numbers representable in the argument type
fn is_perfect(n i64) bool {
... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Wren | Wren | var isPerfect = Fn.new { |n|
if (n <= 2) return false
var tot = 1
for (i in 2..n.sqrt.floor) {
if (n%i == 0) {
tot = tot + i
var q = (n/i).floor
if (q > i) tot = tot + q
}
}
return n == tot
}
System.print("The first four perfect numbers are:")
va... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #AutoHotkey | AutoHotkey | Gui, font, s12
Gui, add, text, w90, Computer:
loop, 3
Gui, add, button, x+10 h30 w30 vCB%A_Index%
Gui, add, edit, xs w240 R3 vSequence
Gui, add, text, w90, Human:
loop, 3
Gui, add, button, x+10 h30 w30 vHB%A_Index% gHumButton, H
Gui, add, button, xm gToss, toss
Gui, add, button, x+10 gReset, Reset
Gui, show,, Penney... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #Ada | Ada | with Ada.Text_IO;
procedure Converging_Sequence is
generic
type Num is digits <>;
After: Positive;
procedure Task_1;
procedure Task_1 is
package FIO is new Ada.Text_IO.Float_IO(Num);
package IIO is new Ada.Text_IO.Integer_IO(Integer);
procedure Output (I: Integer; N: Num) ... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Factor | Factor | USING: formatting kernel locals math math.functions sequences ;
:: solve-pell ( n -- a b )
n sqrt >integer :> x!
x :> y!
1 :> z!
2 x * :> r!
1 0 :> ( e1! e2! )
0 1 :> ( f1! f2! )
0 0 :> ( a! b! )
[ a sq b sq n * - 1 = ] [
r z * y - y!
n y sq - z / floor z!
... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #FreeBASIC | FreeBASIC |
Sub Fun(Byref a As LongInt, Byref b As LongInt, c As Integer)
Dim As LongInt t
t = a : a = b : b = b * c + t
End Sub
Sub SolvePell(n As Integer, Byref a As LongInt, Byref b As LongInt)
Dim As Integer z, r
Dim As LongInt x, y, e1, e2, f1, f2
x = Sqr(n) : y = x : z = 1 : r = 2 * x
e1 = 1 : e... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #PostScript | PostScript | %!PS-Adobe-3.0 EPSF
%%BoundingBox: 0 0 200 600
/n 5 def % 5-star; can be set to other odd numbers
/s { gsave } def
/r { grestore } def
/g { .7 setgray } def
/t { 100 exch translate } def
/p {
180 90 n div sub rotate
0 0 moveto
n { 0 160 rlineto 180 180 n div sub rotate } repeat
closepath
} def
s 570 t p s g e... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Processing | Processing |
//Aamrun, 29th June 2022
size(1000,1000);
translate(width/2,height/2);
rotate(3*PI/2);
fill(#0000ff);
beginShape();
for(int i=0;i<10;i+=2){
vertex(450*cos(i*2*PI/5),450*sin(i*2*PI/5));
}
endShape(CLOSE);
|
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #PicoLisp | PicoLisp | (load "@lib/simul.l")
(permute (1 2 3)) |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #XPL0 | XPL0 | include c:\cxpl\codes; \intrinsic 'code' declarations
func Perfect(N); \Return 'true' if N is a perfect number
int N, S, I, Q;
[S:= 1;
for I:= 2 to sqrt(N) do
[Q:= N/I;
if rem(0)=0 then S:= S+I+Q;
];
return S=N & N#1;
];
int A, N;
[for A:= 1 to 16 do
[N:= (1<<A - 1) * 1<<(A... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Yabasic | Yabasic |
sub isPerfect(n)
if (n < 2) or mod(n, 2) = 1 then return false : endif
// asumimos que los números impares no son perfectos
sum = 0
for i = 1 to n-1
if mod(n,i) = 0 then sum = sum + i : endif
next i
if sum = n then return true else return false : endif
end sub
print "Los primeros 5 n... |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #Action.21 | Action! | DEFINE MAXSIZE="12"
INT ARRAY
angleStack(MAXSIZE)
BYTE ARRAY
depthStack(MAXSIZE),stageStack(MAXSIZE)
BYTE stacksize=[0]
BYTE FUNC IsEmpty()
IF stacksize=0 THEN RETURN (1) FI
RETURN (0)
BYTE FUNC IsFull()
IF stacksize=MAXSIZE THEN RETURN (1) FI
RETURN (0)
PROC Push(INT angle BYTE depth,stage)
IF IsFull... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #BASIC | BASIC |
global jugador, computador, secuencia
jugador = "" : computador = ""
function SecuenciaJugador(secuencia)
do
valido = TRUE
print chr(10) + "Ingresa una secuencia de 3 opciones, cada una de ellas H o T:";
input " > ", secuencia
secuencia = upper(secuencia)
if length(secuencia) <> 3 then valido = FALSE
i... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #Batch_File | Batch File | ::
::Penney's Game Task from Rosetta Code Wiki
::Batch File Implementation
::
::Please Directly Open the Batch File to play...
::
@echo off
setlocal enabledelayedexpansion
title The Penney's Game Batch File
set cpu=0&set you=0
cls
echo.
echo Penney's Game
echo Batch File Implementation
:main
set you_bet=&set cpu_... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #ALGOL_68 | ALGOL 68 | BEGIN
# task 1 #
BEGIN
PR precision 32 PR
print( ( " 32 digit REAL numbers", newline ) );
[ 1 : 100 ]LONG LONG REAL v;
v[ 1 ] := 2;
v[ 2 ] := -4;
FOR n FROM 3 TO UPB v DO v[ n ] := 111 - ( 1130 / v[ n - 1 ] ) + ( 3000 / ( v[ n - 1 ] * v[ n - 2 ] ) ) OD;
FO... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Go | Go | package main
import (
"fmt"
"math/big"
)
var big1 = new(big.Int).SetUint64(1)
func solvePell(nn uint64) (*big.Int, *big.Int) {
n := new(big.Int).SetUint64(nn)
x := new(big.Int).Set(n)
x.Sqrt(x)
y := new(big.Int).Set(x)
z := new(big.Int).SetUint64(1)
r := new(big.Int).Lsh(x, 1)
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Python | Python | import turtle
turtle.bgcolor("green")
t = turtle.Turtle()
t.color("red", "blue")
t.begin_fill()
for i in range(0, 5):
t.forward(200)
t.right(144)
t.end_fill() |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #PowerBASIC | PowerBASIC | #COMPILE EXE
#DIM ALL
GLOBAL a, i, j, k, n AS INTEGER
GLOBAL d, ns, s AS STRING 'dynamic string
FUNCTION PBMAIN () AS LONG
ns = INPUTBOX$(" n =",, "3") 'input n
n = VAL(ns)
DIM a(1 TO n) AS INTEGER
FOR i = 1 TO n: a(i)= i: NEXT
DO
s = " "
FOR i = 1 TO n
d = STR$(a(i))
s = BUIL... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #Zig | Zig |
const std = @import("std");
const expect = std.testing.expect;
const stdout = std.io.getStdOut().outStream();
pub fn main() !void {
var i: u32 = 2;
try stdout.print("The first few perfect numbers are: ", .{});
while (i <= 10_000) : (i += 2) if (propersum(i) == i)
try stdout.print("{} ", .{i});
... |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #AutoHotkey | AutoHotkey | gdip1()
PeanoX := A_ScreenWidth/2 - 100, PeanoY := A_ScreenHeight/2 - 100
Peano(PeanoX, PeanoY, 3**3, 5, 5, Arr:=[])
xmin := xmax := ymin := ymax := 0
for i, point in Arr
{
xmin := A_Index = 1 ? point.x : xmin < point.x ? xmin : point.x
xmax := point.x > xmax ? point.x : xmax
ymin := A_Index = 1 ? point.y : ymin < p... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #BBC_BASIC | BBC BASIC | REM >penney
PRINT "*** Penney's Game ***"
REPEAT
PRINT ' "Heads you pick first, tails I pick first."
PRINT "And it is... ";
WAIT 100
ht% = RND(0 - TIME) AND 1
IF ht% THEN
PRINT "heads!"
PROC_player_chooses(player$)
computer$ = FN_optimal(player$)
PRINT "I choose "; computer$; "."
ELSE
PR... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #AWK | AWK |
BEGIN {
do_task1()
do_task2()
do_task3()
exit
}
function do_task1(){
print "Task 1"
v[1] = 2
v[2] = -4
for (n=3; n<=100; n++) v[n] = 111 - 1130 / v[n-1] + 3000 / (v[n-1] * v[n-2])
for (i=3; i<=8; i++) print_results(i)
print_results(20)
print_results(30)
print_resu... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Haskell | Haskell | pell :: Integer -> (Integer, Integer)
pell n = go (x, 1, x * 2, 1, 0, 0, 1)
where
x = floor $ sqrt $ fromIntegral n
go (y, z, r, e1, e2, f1, f2) =
let y' = r * z - y
z' = (n - y' * y') `div` z
r' = (x + y') `div` z'
(e1', e2') = (e2, e2 * r' + e1)
(f1', f2') = (f2... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Quackery | Quackery | [ $ "turtleduck.qky" loadfile ] now!
[ [ 1 1
30 times
[ tuck + ]
swap join ] constant
do ] is phi ( --> n/d )
[ 5 times
[ 2dup walk
1 5 turn
2dup walk
3 5 turn ]
2drop ] is star ( n/d --> )
[ 5 times
[ 2dup walk
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #R | R | p <- cbind(x = c(0, 1, 2,-0.5 , 2.5 ,0),
y = c(0, 1, 0,0.6, 0.6,0))
plot(p)
lines(p) |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #PowerShell | PowerShell |
function permutation ($array) {
function generate($n, $array, $A) {
if($n -eq 1) {
$array[$A] -join ' '
}
else{
for( $i = 0; $i -lt ($n - 1); $i += 1) {
generate ($n - 1) $array $A
if($n % 2 -eq 0){
$i1, $i2 = $i, ... |
http://rosettacode.org/wiki/Perfect_numbers | Perfect numbers | Write a function which says whether a number is perfect.
A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself.
Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Note: The faster Luca... | #zkl | zkl | fcn isPerfectNumber1(n)
{ n == [1..n-1].filter('wrap(i){ n % i == 0 }).sum(); } |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #C | C |
/*Abhishek Ghosh, 14th September 2018*/
#include <graphics.h>
#include <math.h>
void Peano(int x, int y, int lg, int i1, int i2) {
if (lg == 1) {
lineto(3*x,3*y);
return;
}
lg = lg/3;
Peano(x+(2*i1*lg), y+(2*i1*lg), lg, i1, i2);
Peano(x+((i1-i2+1)*lg), y+((i1+i2)*lg), lg, i1, 1-i2);
Peano(x+lg, y+lg... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #C | C |
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SEQLEN 3
int getseq(char *s)
{
int r = 0;
int i = 1 << (SEQLEN - 1);
while (*s && i) {
switch (*s++) {
case 'H':
case 'h':
r |= i;
break;
case 'T':
... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #C | C |
#include<stdio.h>
#include<gmp.h>
void firstCase(){
mpf_t a,b,c;
mpf_inits(a,b,c,NULL);
mpf_set_str(a,"0.1",10);
mpf_set_str(b,"0.2",10);
mpf_add(c,a,b);
gmp_printf("\n0.1 + 0.2 = %.*Ff",20,c);
}
void pathologicalSeries(){
int n;
mpf_t v1, v2, vn, a1, a2, a3, t2, t3, prod;
mpf_inits(v1,v2,vn, a1,... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #J | J | NB. sqrt representation for continued fraction
sqrt_cf =: 3 : 0
rep=. '' [ 'm d'=. 0 1 [ a =. a0=. <. %: y
while. a ~: +: a0 do.
rep=. rep , a=. <. (a0+m) % d=. d %~ y - *: m=. m -~ a*d
end. a0;rep
)
NB. find x,y such that x^2 - n*y^2 = 1 using continued fractions
pell =: 3 : 0
n =. 1 [ 'a0 as' =. x: &.> sqrt_cf y... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Java | Java |
import java.math.BigInteger;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
public class PellsEquation {
public static void main(String[] args) {
NumberFormat format = NumberFormat.getInstance();
for ( int n : new int[] {61, 109, 181, 277, 8941} ) {
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Racket | Racket | #lang racket
(require 2htdp/image)
(overlay
(star-polygon 100 5 2 "outline" (make-pen "blue" 4 "solid" "round" "round"))
(star-polygon 100 5 2 "solid" "cyan")) |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Raku | Raku | use SVG;
constant $dim = 200;
constant $sides = 5;
my @vertices = map { 0.9 * $dim * cis($_ * τ / $sides) }, ^$sides;
my @points = map |*.reals.fmt("%0.3f"),
flat @vertices[0, 2 ... *], @vertices[1, 3 ... *], @vertices[0];
say SVG.serialize(
svg => [
:width($dim*2), :height($dim*2),
:rec... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #Prolog | Prolog | :- use_module(library(clpfd)).
permut_clpfd(L, N) :-
length(L, N),
L ins 1..N,
all_different(L),
label(L). |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #C.2B.2B | C++ | #include <cmath>
#include <fstream>
#include <iostream>
#include <string>
class peano_curve {
public:
void write(std::ostream& out, int size, int length, int order);
private:
static std::string rewrite(const std::string& s);
void line(std::ostream& out);
void execute(std::ostream& out, const std::stri... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #C.23 | C# | using static System.Console;
using static System.Threading.Thread;
using System;
public static class PenneysGame
{
const int pause = 500;
const int N = 3;
static Random rng = new Random();
static int Toss() => rng.Next(2);
static string AsString(this int sequence) {
string s = "";
... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #C.23 | C# | #define USE_BIGRATIONAL
#define BANDED_ROWS
#define INCREASED_LIMITS
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Numerics;
using Numerics;
using static Common;
using static Task1;
using static Task2;
using static Task3;
#if ... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #jq | jq | # If $j is 0, then an error condition is raised;
# otherwise, assuming infinite-precision integer arithmetic,
# if the input and $j are integers, then the result will be an integer.
def idivide($i; $j):
($i % $j) as $mod
| ($i - $mod) / $j ;
def idivide($j):
idivide(.; $j);
# input should be a non-negative inte... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Julia | Julia | function pell(n)
x = BigInt(floor(sqrt(n)))
y, z, r = x, BigInt(1), x << 1
e1, e2, f1, f2 = BigInt(1), BigInt(0), BigInt(0), BigInt(1)
while true
y = r * z - y
z = div(n - y * y, z)
r = div(x + y, z)
e1, e2 = e2, e2 * r + e1
f1, f2 = f2, f2 * r + f1
a, b =... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Red | Red | Red [
Source: https://github.com/vazub/rosetta-red
Tabs: 4
Needs: 'View
]
canvas: 500x500
center: as-pair canvas/x / 2 canvas/y / 2
radius: 200
points: collect [
repeat vertex 10 [
angle: vertex * 36 + 18 ;-- +18 is required for pentagram rotation
either vertex // 2 = 1 [
keep as-pair (co... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #REXX | REXX | /* REXX ***************************************************************
* Create a BMP file showing a pentagram
**********************************************************************/
Parse Version v
If pos('Regina',v)>0 Then
pentagram='pentagrama.bmp'
Else
pentagram='pentagramx.bmp'
'erase' pentagram
s='424d460000... |
http://rosettacode.org/wiki/Password_generator | Password generator | Create a password generation program which will generate passwords containing random ASCII characters from the following groups:
lower-case letters: a ──► z
upper-case letters: A ──► Z
digits: 0 ──► 9
other printable characters: !"#$%&'()*+,-./:;<=>?@[]^_{|}~
(the above ch... | #8086_Assembly | 8086 Assembly | cpu 8086
bits 16
;;; MS-DOS syscalls
gettim: equ 2Ch ; Get system time
write: equ 40h ; Write to file
exit: equ 4Ch ; Exit with return code
;;; MS-DOS process data
arg: equ 80h ; Command line argument (length + string)
;;; BIOS calls
conout: equ 0Eh ; Write character to console
vstate: equ 0Fh ; Get current video ... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #PureBasic | PureBasic | Macro reverse(firstIndex, lastIndex)
first = firstIndex
last = lastIndex
While first < last
Swap cur(first), cur(last)
first + 1
last - 1
Wend
EndMacro
Procedure nextPermutation(Array cur(1))
Protected first, last, elementCount = ArraySize(cur())
If elementCount < 1
ProcedureReturn #False... |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #Factor | Factor | USING: accessors L-system ui ;
: peano ( L-system -- L-system )
L-parser-dialect >>commands
[ 90 >>angle ] >>turtle-values
"L" >>axiom
{
{ "L" "LFRFL-F-RFLFR+F+LFRFL" }
{ "R" "RFLFR+F+LFRFL-F-RFLFR" }
} >>rules ;
[ <L-system> peano "Peano curve" open-window ] with-ui |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #F.C5.8Drmul.C3.A6 | Fōrmulæ | Const anchura = 243 'una potencia de 3 para una curva uniformemente espaciada
Screenres 700,700
Sub Peano(x As Integer, y As Integer, lg As Integer, i1 As Integer, i2 As Integer)
If lg = 1 Then
Line - (x * 3, y * 3)
Return
End If
lg /= 3
Peano(x + (2 * i1 * lg), y + (2 * i1 * lg), lg... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #C.2B.2B | C++ |
#include <time.h>
#include <iostream>
#include <string>
using namespace std;
class penney
{
public:
penney()
{ pW = cW = 0; }
void gameLoop()
{
string a;
while( true )
{
playerChoice = computerChoice = "";
if( rand() % 2 )
{ computer(); player(); }
else
{ player(); co... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #Clojure | Clojure | (def converge-to-six ((fn task1 [a b] (lazy-seq (cons a (task1 b (+ (- 111 (/ 1130 b)) (/ 3000 (* b a))))))) 2 -4))
(def values [3 4 5 6 7 8 20 30 50 100])
; print decimal values:
(pprint (sort (zipmap values (map double (map #(nth converge-to-six (dec %)) values)))))
; print rational values:
(pprint (sort (zipmap valu... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Kotlin | Kotlin | import java.math.BigInteger
import kotlin.math.sqrt
class BIRef(var value: BigInteger) {
operator fun minus(b: BIRef): BIRef {
return BIRef(value - b.value)
}
operator fun times(b: BIRef): BIRef {
return BIRef(value * b.value)
}
override fun equals(other: Any?): Boolean {
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Ring | Ring |
# Project : Pentagram
load "guilib.ring"
paint = null
new qapp
{
win1 = new qwidget() {
setwindowtitle("Pentagram")
setgeometry(100,100,500,600)
label1 = new qlabel(win1) {
setgeometry(10,10,400,400)
... |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Scala | Scala | import java.awt._
import java.awt.geom.Path2D
import javax.swing._
object Pentagram extends App {
SwingUtilities.invokeLater(() =>
new JFrame("Pentagram") {
class Pentagram extends JPanel {
setPreferredSize(new Dimension(640, 640))
setBackground(Color.white)
final private val... |
http://rosettacode.org/wiki/Password_generator | Password generator | Create a password generation program which will generate passwords containing random ASCII characters from the following groups:
lower-case letters: a ──► z
upper-case letters: A ──► Z
digits: 0 ──► 9
other printable characters: !"#$%&'()*+,-./:;<=>?@[]^_{|}~
(the above ch... | #Action.21 | Action! | BYTE FUNC GetNumParam(CHAR ARRAY text BYTE min,max)
BYTE res
DO
PrintF("Input %S (%B-%B): ",text,min,max)
res=InputB()
UNTIL res>=min AND res<=max
OD
RETURN (res)
BYTE FUNC GetBoolParam(CHAR ARRAY text)
CHAR ARRAY s(255)
DO
PrintF("%S? (Y/N): ",text)
InputS(s)
IF s(0)=1 THEN
... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #Python | Python | import itertools
for values in itertools.permutations([1,2,3]):
print (values) |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #FreeBASIC | FreeBASIC | Const anchura = 243 'una potencia de 3 para una curva uniformemente espaciada
Screenres 700,700
Sub Peano(x As Integer, y As Integer, lg As Integer, i1 As Integer, i2 As Integer)
If lg = 1 Then
Line - (x * 3, y * 3)
Return
End If
lg /= 3
Peano(x + (2 * i1 * lg), y + (2 * i1 * lg), lg... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #Clojure | Clojure | (ns penney.core
(:gen-class))
(def heads \H)
(def tails \T)
(defn flip-coin []
(let [flip (rand-int 2)]
(if (= flip 0) heads tails)))
(defn turn [coin]
(if (= coin heads) tails heads))
(defn first-index [combo coll]
(some #(if (= (second %) combo) (first %)) coll))
(defn find-winner [h c]
(if (<... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #Crystal | Crystal | require "big"
ar = [0.to_big_d, 2.to_big_d, -4.to_big_d]
100.times { ar << 111 - 1130.to_big_d.div(ar[-1], 132) + 3000.to_big_d.div((ar[-1] * ar[-2]), 132) }
[3, 4, 5, 6, 7, 8, 20, 30, 50, 100].each do |n|
puts "%3d -> %0.16f" % [n, ar[n]]
end
|
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #langur | langur | val .fun = f [.b, .b x .c + .a]
val .solvePell = f(.n) {
val .x = truncate .n ^/ 2
var .y, .z, .r = .x, 1, .x x 2
var .e1, .e2, .f1, .f2 = 1, 0, 0, 1
for {
.y = .r x .z - .y
.z = (.n - .y x .y) \ .z
.r = (.x + .y) \ .z
.e1, .e2 = .fun(.e1, .e2, .r)
.f1, .f2 = ... |
http://rosettacode.org/wiki/Pell%27s_equation | Pell's equation | Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form:
x2 - ny2 = 1
with integer solutions for x and y, where n is a given non-square positive integer.
Task requirements
find the smallest solution in positive integers to Pell's equation for... | #Mathematica.2FWolfram_Language | Mathematica/Wolfram Language | FindInstance[x^2 - 61 y^2 == 1, {x, y}, PositiveIntegers]
FindInstance[x^2 - 109 y^2 == 1, {x, y}, PositiveIntegers]
FindInstance[x^2 - 181 y^2 == 1, {x, y}, PositiveIntegers]
FindInstance[x^2 - 277 y^2 == 1, {x, y}, PositiveIntegers] |
http://rosettacode.org/wiki/Pentagram | Pentagram |
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.
Task
Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and backg... | #Sidef | Sidef | func pentagram(dim=200, sides=5) {
var pentagram = <<-EOT
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/PR-SVG-20010719/DTD/svg10.dtd">
<svg height="#{dim*2}" width="#{dim*2}" style="" xmlns="http://www.w3.org/2000/svg">
<rect he... |
http://rosettacode.org/wiki/Password_generator | Password generator | Create a password generation program which will generate passwords containing random ASCII characters from the following groups:
lower-case letters: a ──► z
upper-case letters: A ──► Z
digits: 0 ──► 9
other printable characters: !"#$%&'()*+,-./:;<=>?@[]^_{|}~
(the above ch... | #Ada | Ada | with Ada.Numerics.Discrete_Random;
with Ada.Strings.Fixed;
with Ada.Command_Line;
with Ada.Integer_Text_IO;
with Ada.Text_IO;
procedure Mkpw is
procedure Put_Usage;
procedure Parse_Command_Line (Success : out Boolean);
function Create_Password (Length : in Positive;
Safe : in... |
http://rosettacode.org/wiki/Permutations | Permutations | Task
Write a program that generates all permutations of n different objects. (Practically numerals!)
Related tasks
Find the missing permutation
Permutations/Derangements
The number of samples of size k from n objects.
With combinations and permutations generation tasks.
Order Uni... | #QBasic | QBasic | SUB perms (n)
DIM a(0 TO n - 1), c(0 TO n - 1)
FOR j = 0 TO n - 1
a(j) = j + 1
PRINT a(j);
NEXT j
PRINT
i = 0
WHILE i < n
IF c(i) < i THEN
IF (i AND 1) = 0 THEN
SWAP a(0), a(i)
ELSE
SWAP a(c(i)), a(i)
... |
http://rosettacode.org/wiki/Peano_curve | Peano curve |
Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
| #Go | Go | package main
import "github.com/fogleman/gg"
var points []gg.Point
const width = 81
func peano(x, y, lg, i1, i2 int) {
if lg == 1 {
px := float64(width-x) * 10
py := float64(width-y) * 10
points = append(points, gg.Point{px, py})
return
}
lg /= 3
peano(x+2*i1*lg, ... |
http://rosettacode.org/wiki/Penney%27s_game | Penney's game | Penney's game is a game where two players bet on being the first to see a particular sequence of heads or tails in consecutive tosses of a fair coin.
It is common to agree on a sequence length of three then one player will openly choose a sequence, for example:
Heads, Tails, Heads, or HTH for short.
The other pla... | #Common_Lisp | Common Lisp | (setf *random-state* (make-random-state t))
(defparameter *heads* #\H)
(defparameter *tails* #\T)
(defun main ()
(format t "Penney's Game~%~%")
(format t "Flipping to see who goes first ...")
(setq p2 nil)
(if (string= (flip) *heads*)
(progn (format t " I do.~%")
(setq p2 (choose-random... |
http://rosettacode.org/wiki/Pathological_floating_point_problems | Pathological floating point problems | Most programmers are familiar with the inexactness of floating point calculations in a binary processor.
The classic example being:
0.1 + 0.2 = 0.30000000000000004
In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding.
There are pathological pro... | #Delphi | Delphi |
program Pathological_floating_point_problems;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
Velthuis.BigIntegers,
Velthuis.BigRationals,
Velthuis.BigDecimals;
type
TBalance = record
e, d: BigInteger;
end;
procedure Sequence(Precision: Integer);
var
v, v1, c111, c1130, c3000, t2, t3: BigRational;
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.