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/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Go | Go | package main
import "fmt"
func gcd(a, b uint) uint {
if b == 0 {
return a
}
return gcd(b, a%b)
}
func lcm(a, b uint) uint {
return a / gcd(a, b) * b
}
func ipow(x, p uint) uint {
prod := uint(1)
for p > 0 {
if p&1 != 0 {
prod *= x
}
p >>= 1
... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #6502_Assembly | 6502 Assembly | define color $00
define looptemp $01
lda #1
sta color
loop_1wide:
lda color
and #$01
; this takes advantage of the fact that Easy6502 maps black to 0 and white to 1.
; Thus if we clear all but bit 0 the color will be either black or white, alternating infinitely regardless of the actual value
; of the color variable... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Oforth | Oforth | Integer method: isPrime
| i |
self 1 <= ifTrue: [ false return ]
self 3 <= ifTrue: [ true return ]
self isEven ifTrue: [ false return ]
3 self sqrt asInteger for: i [ self i mod ifzero: [ false return ] ]
true ; |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #Tcl | Tcl | proc factors {x} {
# list the prime factors of x in ascending order
set result [list]
while {$x % 2 == 0} {
lappend result 2
set x [expr {$x / 2}]
}
for {set i 3} {$i*$i <= $x} {incr i 2} {
while {$x % $i == 0} {
lappend result $i
set x [expr {$x / $i}... |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #Python | Python | import time
from pygame import mixer
mixer.init(frequency=16000) #set frequency for wav file
s1 = mixer.Sound('test.wav')
s2 = mixer.Sound('test2.wav')
#individual
s1.play(-1) #loops indefinitely
time.sleep(0.5)
#simultaneously
s2.play() #play once
time.sleep(2)
s2.play(2) #optional para... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Haskell | Haskell | import qualified Data.Text as T
main = do
putStrLn "PisanoPrime(p,2) for prime p lower than 15"
putStrLn . see 15 . map (`pisanoPrime` 2) . filter isPrime $ [1 .. 15]
putStrLn "PisanoPrime(p,1) for prime p lower than 180"
putStrLn . see 15 . map (`pisanoPrime` 1) . filter isPrime $ [1 .. 180]
let ns = [1 ..... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #8086_Assembly | 8086 Assembly | ;;; Display pinstripes on a PC, using 8086 assembly.
;;; The 640x200 CGA video mode is used. If you are on an MDA, the
;;; program does not run.
bits 16
cpu 8086
;;; IBM BIOS (INT 10h) calls
vmode: equ 0Fh ; Get current video mode
;;; Video modes
MDATXT: equ 7 ; MDA text mode (to check current mode against)
CGAH... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Ol | Ol |
(define (prime? number)
(define max (sqrt number))
(define (loop divisor)
(or (> divisor max)
(and (> (modulo number divisor) 0)
(loop (+ divisor 2)))))
(or (= number 1)
(= number 2)
(and
(> (modulo number 2) 0)
(loop 3))))
|
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #TI-83_BASIC | TI-83 BASIC | ::prgmPREMIER
Disp "FACTEURS PREMIER"
Prompt N
If N<1:Stop
ClrList L1�,L2
0→K
iPart(√(N))→L
N→M
For(I,2,L)
0→J
While fPart(M/I)=0
J+1→J
M/I→M
End
If J≠0
Then
K+1→K
I→L�1(K)
J→L2(K)
I→Z:prgmVSTR
" "+Str0→Str1
If J≠1
Then
J→Z:prgmVSTR
Str1+"^"+Str0→Str1
End
Disp Str1
End
If M=1:Stop
End
If M≠1
Then
If M≠N
Then
M→Z:prgm... |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #R | R |
#Setup
# Warning: The example files are Windows specific.
library(sound)
media_dir <- file.path(Sys.getenv("SYSTEMROOT"), "Media")
chimes <- loadSample(file.path(media_dir, "chimes.wav"))
chord <- loadSample(file.path(media_dir, "chord.wav"))
# Play sequentially
play(appendSample(chimes, chord))
# Play simultaneo... |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #Racket | Racket |
#lang racket/gui
(play-sound "some-sound.wav" #f)
|
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #Ring | Ring |
Load "guilib.ring"
new qapp {
q1=NULL q2=NULL
win1 = new qwidget() {
setwindowtitle("play sound!") show()
setgeometry(100,100,400,400)
}
new qpushbutton(win1) {
setgeometry(50,50,100,30)
settext("play1")
setcli... |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #Ruby | Ruby | require 'win32/sound'
include Win32
sound1 = ENV['WINDIR'] + '\Media\Windows XP Startup.wav'
sound2 = ENV['WINDIR'] + '\Media\Windows XP Shutdown.wav'
puts "play the sounds sequentially"
[sound1, sound2].each do |s|
t1 = Time.now
Sound.play(s)
puts "'#{s}' duration: #{(Time.now.to_f - t1.to_f)} seconds"
en... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Java | Java |
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class PisanoPeriod {
public static void main(String[] args) {
System.out.printf("Print pisano(p^2) for every prime p lower than 15%n");
... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Action.21 | Action! | PROC Main()
BYTE
CH=$02FC, ;Internal hardware value for last key pressed
COLOR0=$02C4,COLOR1=$02C5,COLOR2=$02C6,COLOR4=$02C8
CARD i
Graphics(8+16)
COLOR4=$04 ;gray
COLOR1=$00 ;black
COLOR2=$0F ;white
FOR i=0 TO 319
DO
Color=i MOD 2
Plot(i,0) DrawTo(i,47)
Color=i/2 MOD 2
Plo... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #ActionScript | ActionScript |
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
public class Pinstripe extends Sprite {
public function Pinstripe():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Oz | Oz | fun {Prime N}
local IPrime in
fun {IPrime N Acc}
if N < Acc*Acc then true
elseif (N mod Acc) == 0 then false
else {IPrime N Acc+1}
end
end
if N < 2 then false
else {IPrime N 2} end
end
end |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #Tiny_BASIC | Tiny BASIC | 10 PRINT "Enter a number."
20 INPUT N
25 PRINT "------"
30 IF N<0 THEN LET N = -N
40 IF N<2 THEN END
50 LET I = 2
60 IF I*I > N THEN GOTO 200
70 IF (N/I)*I = N THEN GOTO 300
80 LET I = I + 1
90 GOTO 60
200 PRINT N
210 END
300 LET N = N / I
310 PRINT I
320 GOTO 50 |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #Swift | Swift | import AVFoundation
// This example uses AVAudioPlayer for playback.
// AVAudioPlayer is the player Apple recommends for playback, since it suitable for songs
// and offers control over numerous playback parameters.
// It can play any type that is natively supported by OSX or iOS
class PlayerControl: NSObject, AVAu... |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #Tcl | Tcl | package require sound
# Potentially also require driver support for particular formats
# Load some sounds in; this part can be slow
snack::sound s1
s1 read $soundFile1
snack::sound s2
s2 read $soundFile2
# Play a sound for a while (0.1 seconds; this is a low-latency operation)
s1 play
after 100 set done 1; vwait do... |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #TUSCRIPT | TUSCRIPT |
$$ MODE TUSCRIPT
audiofile="test.wav"
ERROR/STOP OPEN (audiofile,READ,-std-)
BROWSE $audiofile
|
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Julia | Julia | using Primes
const pisanos = Dict{Int, Int}()
function pisano(p)
p < 2 && return 1
(i = get(pisanos, p, 0)) > 0 && return i
lastn, n = 0, 1
for i in 1:p^2
lastn, n = n, (lastn + n) % p
if lastn == 0 && n == 1
pisanos[p] = i
return i
end
end
retur... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Nim | Nim | import math, strformat, tables
func primes(n: Positive): seq[int] =
## Return the list of prime divisors of "n".
var n = n.int
for d in 2..n:
var q = n div d
var m = n mod d
while m == 0:
result.add d
n = q
q = n div d
m = n mod d
func isPrime(n: Positive): bool =
## Retu... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Ada | Ada | with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Events.Events;
procedure Pinstripe_Display is
Width : constant := 800;
Height : constant := 400;
Window : SDL.Video.Windows.Window;
Renderer : SDL.Video.Renderers.Renderer;
Event : SDL.Events.Events.Events;
use SD... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Panda | Panda | fun prime(p) type integer->integer
p.gt(1) where q=p.sqrt NO(p.mod(2..q)==0)
1..100.prime |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #TXR | TXR | @(next :args)
@(do
(defun factor (n)
(if (> n 1)
(for ((max-d (isqrt n))
(d 2))
()
((inc d (if (evenp d) 1 2)))
(cond ((> d max-d) (return (list n)))
((zerop (mod n d))
(return (cons d (factor (trunc n d))))))))))
@{num /[0-9]+/}
@(bin... |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #UNIX_Shell | UNIX Shell | #!/usr/bin/sh
# play.sh
# Plays .au files.
# Usage: play.sh <recorded_sound.au>
cat $1 >> /dev/audio # Write file $1 to the speaker's Character Special (/dev/audio). |
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #VBA | VBA |
Declare Function libPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal filename As String, ByVal Flags As Long) As Long
Sub PlaySound(sWav As String)
Call libPlaySound(sWav, 1) '1 to play asynchronously
End Sub
|
http://rosettacode.org/wiki/Play_recorded_sounds | Play recorded sounds | Load at least two prerecorded sounds, and demonstrate as many of these features as you can:
playing them individually and simultaneously
stopping before the end of the sound
looping (preferably glitch-free)
setting the volume of each sound
stereo or 3D positional mixing
performing other actions when marked times... | #Wren | Wren | import "audio" for AudioEngine
import "dome" for Process
class Game {
static init() {
// load a .wav file and give it a friendly name
AudioEngine.load("a", "a.wav")
// play the file at volume v, looping l and pan p
__v = 2 // twice 'normal' volume
__l = true // loop ... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Perl | Perl | use strict;
use warnings;
use feature 'say';
use ntheory qw(primes factor_exp lcm);
sub pisano_period_pp {
my($a, $b, $n, $k) = (0, 1, $_[0]**$_[1]);
while (++$k) {
($a, $b) = ($b, ($a+$b) % $n);
return $k if $a == 0 and $b == 1;
}
}
sub pisano_period {
(lcm map { pisano_period_pp($$... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #AutoHotkey | AutoHotkey | h := A_ScreenHeight
w := A_ScreenWidth
pToken := Gdip_Startup()
hdc := CreateCompatibleDC()
hbm := CreateDIBSection(w, h)
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
OnExit, Exit
Gui -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
Gui Show, NA
hwnd := WinExist()
pBrushB := Gdip_Brus... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #PARI.2FGP | PARI/GP | trial(n)={
if(n < 4, return(n > 1)); /* Handle negatives */
forprime(p=2,sqrtint(n),
if(n%p == 0, return(0))
);
1
}; |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #V | V | [prime-decomposition
[inner [c p] let
[c c * p >]
[p unit]
[ [p c % zero?]
[c c p c / inner cons]
[c 1 + p inner]
ifte]
ifte].
2 swap inner]. |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Phix | Phix | with javascript_semantics
function pisano_period(integer m)
-- Calculates the Pisano period of 'm' from first principles. (copied from Go)
integer p = 0, c = 1
for i=0 to m*m-1 do
{p, c} = {c, mod(p+c,m)}
if p == 0 and c == 1 then
return i + 1
end if
end for
return 1
... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #BBC_BASIC | BBC BASIC | GWL_STYLE = -16
HWND_TOPMOST = -1
WS_VISIBLE = &10000000
WS_CLIPCHILDREN = &2000000
WS_CLIPSIBLINGS = &4000000
SYS "GetSystemMetrics", 0 TO xscreen%
SYS "GetSystemMetrics", 1 TO yscreen%
SYS "SetWindowLong", @hwnd%, GWL_STYLE, WS_VISIBLE + \
\ W... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Befunge | Befunge | "%":*3-"`"8*>4/::8%00p8/10p4*\55+"1P",,v
,:.\.5vv-g025:\-1_$$55+,\:v1+*8g01g00_@>
024,+5<>/2%.1+\:>^<:\0:\-1_$20g1-:20p^1p |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Pascal | Pascal | program primes;
function prime(n: integer): boolean;
var
i: integer; max: real;
begin
if n = 2 then
prime := true
else if (n <= 1) or (n mod 2 = 0) then
prime := false
else begin
prime := true; i := 3; max := sqrt(n);
while i <= max do begin
if n mod i = 0 then begin
prime := fal... |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #VBScript | VBScript | Function PrimeFactors(n)
arrP = Split(ListPrimes(n)," ")
divnum = n
Do Until divnum = 1
'The -1 is to account for the null element of arrP
For i = 0 To UBound(arrP)-1
If divnum = 1 Then
Exit For
ElseIf divnum Mod arrP(i) = 0 Then
divnum... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #AWK | AWK |
#!/usr/bin/awk -f
function clamp(val, a, b) { return (val<a) ? a : (val>b) ? b : val }
## return a timestamp with centisecond precision
function timex() {
getline < "/proc/uptime"
close("/proc/uptime")
return $1
}
## draw image to terminal
function draw(src, xpos, ypos, w,h, x,y, up,dn, line,screen) {
... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Python | Python | from sympy import isprime, lcm, factorint, primerange
from functools import reduce
def pisano1(m):
"Simple definition"
if m < 2:
return 1
lastn, n = 0, 1
for i in range(m ** 2):
lastn, n = n, (lastn + n) % m
if lastn == 0 and n == 1:
return i + 1
return 1
de... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #C | C |
#include<graphics.h>
#include<conio.h>
#define sections 4
int main()
{
int d=DETECT,m,maxX,maxY,x,y,increment=1;
initgraph(&d,&m,"c:/turboc3/bgi");
maxX = getmaxx();
maxY = getmaxy();
for(y=0;y<maxY;y+=maxY/sections)
{
for(x=0;x<maxX;x+=increment)
{
setfillstyle(SOLID_FILL,(x/increment)%2==0?BLAC... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Perl | Perl | sub prime { my $n = shift || $_;
$n % $_ or return for 2 .. sqrt $n;
$n > 1
}
print join(', ' => grep prime, 1..100), "\n"; |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #Wren | Wren | import "/big" for BigInt
import "/fmt" for Fmt
var vals = [1 << 31, 1234567, 333333, 987653, 2 * 3 * 5 * 7 * 11 * 13 * 17]
for (val in vals) {
Fmt.print("$10d -> $n", val, BigInt.primeFactors(val))
} |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #C | C |
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#define pi M_PI
int main()
{
CONSOLE_SCREEN_BUFFER_INFO info;
int cols, rows;
time_t t;
int i,j;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
cols = info.srWindow.Right - info.srWindo... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #11l | 11l | F uniq(seq)
[Char] seen
L(x) seq
I x !C seen
seen.append(x)
R seen
F partition(seq, n)
R (0 .< seq.len).step(n).map(i -> @seq[i .< i + @n])
F canonicalize(s)
R s.uppercase().filter(c -> c.is_uppercase()).join(‘’).replace(‘J’, ‘I’)
T Playfair
[String = String] dec, enc
F (key)... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Raku | Raku | use Prime::Factor;
constant @fib := 1,1,*+*…*;
my %cache;
multi pisano-period (Int $p where *.is-prime, Int $k where * > 0 = 1) {
return %cache{"$p|$k"} if %cache{"$p|$k"};
my $fibmod = @fib.map: * % $p**$k;
%cache{"$p|$k"} = (1..*).first: { !$fibmod[$_-1] and ($fibmod[$_] == 1) }
}
multi pisano-per... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #C.23 | C# |
using System.Drawing;
public class Pinstripe
{
static void Main(string[] args)
{
var pinstripe = MakePinstripeImage(1366, 768);
pinstripe.Save("pinstripe.png");
}
public static Bitmap MakePinstripeImage(int width, int height)
{
var image = new Bitmap(width, height);
... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Phix | Phix | function is_prime_by_trial_division(integer n)
if n<2 then return 0 end if
if n=2 then return 1 end if
if remainder(n,2)=0 then return 0 end if
for i=3 to floor(sqrt(n)) by 2 do
if remainder(n,i)=0 then
return 0
end if
end for
return 1
end function
?filter(tagset(32),... |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #XSLT | XSLT | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/numbers">
<html>
<body>
<ul>
<xsl:apply-templates />
</ul>
</body>
</html>
</xsl:template>
<xsl:template match... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #C.2B.2B | C++ |
#include <windows.h>
#include <math.h>
#include <string>
const int BMP_SIZE = 240, MY_TIMER = 987654;
class myBitmap {
public:
myBitmap() : pen( NULL ), brush( NULL ), clr( 0 ), wid( 1 ) {}
~myBitmap() {
DeleteObject( pen ); DeleteObject( brush );
DeleteDC( hdc ); DeleteObject( bmp );
... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #C.2B.2B | C++ | #include <iostream>
#include <string>
using namespace std;
class playfair
{
public:
void doIt( string k, string t, bool ij, bool e )
{
createGrid( k, ij ); getTextReady( t, ij, e );
if( e ) doIt( 1 ); else doIt( -1 );
display();
}
private:
void doIt( int dir )
{
int a, b, c, d; string ntxt... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #REXX | REXX | /*REXX pgm calculates pisano period for a range of N, and pisanoPrime(N,m) [for primes]*/
numeric digits 500 /*ensure enough decimal digits for Fib.*/
parse arg lim.1 lim.2 lim.3 . /*obtain optional arguments from the CL*/
if lim.1=='' | lim.1=="," then lim.1= 15 - 1 ... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #C.2B.2B | C++ |
#include <windows.h>
//--------------------------------------------------------------------------------------------------
class pinstripe
{
public:
pinstripe() { createColors(); }
void setDimensions( int x, int y ) { _mw = x; _mh = y; }
void createColors()
{
colors[0] = 0; co... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #PHP | PHP | <?php
function prime($a) {
if (($a % 2 == 0 && $a != 2) || $a < 2)
return false;
$limit = sqrt($a);
for ($i = 2; $i <= $limit; $i++)
if ($a % $i == 0)
return false;
return true;
}
foreach (range(1, 100) as $x)
if (prime($x)) echo "$x\n";
?> |
http://rosettacode.org/wiki/Prime_decomposition | Prime decomposition | The prime decomposition of a number is defined as a list of prime numbers
which when all multiplied together, are equal to that number.
Example
12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
Task
Write a function which returns an array or collection which contains the prime decomposition of a given ... | #zkl | zkl | fcn primeFactors(n){ // Return a list of factors of n
acc:=fcn(n,k,acc,maxD){ // k is 2,3,5,7,9,... not optimum
if(n==1 or k>maxD) acc.close();
else{
q,r:=n.divr(k); // divr-->(quotient,remainder)
if(r==0) return(self.fcn(q,k,acc.write(k),q.toFloat().sqrt()));
return(self.fcn(n,k+1+k.i... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Ceylon | Ceylon |
import javafx.application {
Application
}
import javafx.stage {
Stage
}
import javafx.scene {
Scene
}
import javafx.scene.layout {
BorderPane
}
import javafx.scene.image {
WritableImage,
ImageView
}
import ceylon.numeric.float {
sin,
sqrt,
remainder
}
import javafx.scene.paint {
... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Common_Lisp | Common Lisp | (require :lispbuilder-sdl)
(require :simple-rgb)
(defparameter *palette*
(let ((palette-aux (make-array 256 :element-type 'fixnum)))
(dotimes (i 256)
(let ((color_i (simple-rgb:hsv->rgb (simple-rgb:hsv (/ i 255.0) 1.0 1.0))))
(setf (aref palette-aux i) (loop :for component :across color_i
... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #D | D | import std.stdio, std.array, std.algorithm, std.range, std.ascii,
std.conv, std.string, std.regex, std.typecons;
string unique(in string s) pure nothrow @safe {
string result;
foreach (immutable char c; s)
if (!result.representation.canFind(c)) // Assumes ASCII string.
result ~= c;
... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Sidef | Sidef | func pisano_period_pp(p,k) is cached {
assert(k.is_pos, "k = #{k} must be positive")
assert(p.is_prime, "p = #{p} must be prime")
var (a, b, n) = (0, 1, p**k)
1..Inf -> first_by {
(a, b) = (b, (a+b) % n)
(a == 0) && (b == 1)
}
}
func pisano_period(n) {
n.factor_map {|p,... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #FreeBASIC | FreeBASIC | ' version 14-03-2017
' compile with: fbc -s console
' or compile with: fbc -s gui
Dim As UInteger ps, col, h, w, x, y1, y2
ScreenInfo w, h
' create display size window, 8bit color (palette), no frame
ScreenRes w, h, 8,, 8
' vga palette black = 0 and white = 15
h = h \ 4 : y2 = h -1
For ps = 1 To 4
col =... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Picat | Picat | is_prime1(N) =>
if N == 2 then
true
elseif N <= 1 ; N mod 2 == 0 then
false
else
foreach(I in 3..2..ceiling(sqrt(N)))
N mod I > 0
end
end. |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Forth | Forth | : sqrt ( u -- sqrt ) ( Babylonian method )
dup 2/ ( first square root guess is half )
dup 0= if drop exit then ( sqrt[0]=0, sqrt[1]=1 )
begin dup >r 2dup / r> + 2/ ( stack: square old-guess new-guess )
2dup > while ( as lon... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #FreeBASIC | FreeBASIC | ' version 12-04-2017
' compile with: fbc -s gui
' Computer Graphics Tutorial (lodev.org), last example
#Define dist(a, b, c, d) Sqr(((a - c) * (a - c) + (b - d) * (b - d)))
Const As ULong w = 256
Const As ULong h = 256
ScreenRes w, h, 24
WindowTitle ("Plasma effect")
Dim As ULong x, y
Dim As UByte c
Dim As Double... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #FreeBASIC | FreeBASIC | ' FB 1.05.0 Win64
Enum PlayFairOption
noQ
iEqualsJ
End Enum
Dim Shared pfo As PlayFairOption '' set this before calling buildTable
Dim Shared table(1 To 5, 1 To 5) As UInteger
Sub buildTable(keyword As String)
Dim used(1 To 26) As Boolean '' all false by default
If pfo = noQ Then
used(17) = True
... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #Wren | Wren | import "/math" for Int
import "/fmt" for Fmt
// Calculates the Pisano period of 'm' from first principles.
var pisanoPeriod = Fn.new { |m|
var p = 0
var c = 1
for (i in 0...m*m) {
var t = p
p = c
c = (t + c) % m
if (p == 0 && c == 1) return i + 1
}
return 1
}
// C... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Gambas | Gambas | 'WARNING this takes a time to display
'Use the 'gb.qt4' component
Public Sub Form_Open()
Dim iColour As Integer[] = [Color.Black, Color.white]
Dim hPanel As Panel
Dim siCount, siCounter, siSet As Short
With Me
.Arrangement = Arrange.Row
.Border = False
.Height = Desktop.Height
.Width = Desktop.Width
.Full... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #PicoLisp | PicoLisp | (de prime? (N)
(or
(= N 2)
(and
(> N 1)
(bit? 1 N)
(let S (sqrt N)
(for (D 3 T (+ D 2))
(T (> D S) T)
(T (=0 (% N D)) NIL) ) ) ) ) ) |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Go | Go | $ convert plasma.gif -coalesce plasma2.gif
$ eog plasma2.gif
|
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #Go | Go | package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type playfairOption int
const (
noQ playfairOption = iota
iEqualsJ
)
type playfair struct {
keyword string
pfo playfairOption
table [5][5]byte
}
func (p *playfair) init() {
// Build table.
var used [26]bool ... |
http://rosettacode.org/wiki/Pisano_period | Pisano period | The Fibonacci sequence taken modulo 2 is a periodic sequence of period 3 : 0, 1, 1, 0, 1, 1, ...
For any integer n, the Fibonacci sequence taken modulo n is periodic and the length of the periodic cycle is referred to as the Pisano period.
Prime numbers are straightforward; the Pisano period of a prime number p is si... | #zkl | zkl | var [const] BI=Import("zklBigNum"); // libGMP
fcn pisanoPeriod(p){
if(p<2) return(0);
lastn,n,t := 0,1,0;
foreach i in ([0..p*p]){
t,n,lastn = n, (lastn + n) % p, t;
if(lastn==0 and n==1) return(i + 1);
}
1
}
fcn pisanoPrime(p,k){
_assert_(BI(p).probablyPrime(), "%s is not a prime numbe... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Go | Go | package main
import "github.com/fogleman/gg"
var palette = [2]string{
"FFFFFF", // white
"000000", // black
}
func pinstripe(dc *gg.Context) {
w := dc.Width()
h := dc.Height() / 4
for b := 1; b <= 4; b++ {
for x, ci := 0, 0; x < w; x, ci = x+b, ci+1 {
dc.SetHexColor(pal... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #PL.2FI | PL/I | is_prime: procedure (n) returns (bit(1));
declare n fixed (15);
declare i fixed (10);
if n < 2 then return ('0'b);
if n = 2 then return ('1'b);
if mod(n, 2) = 0 then return ('0'b);
do i = 3 to sqrt(n) by 2;
if mod(n, i) = 0 then return ('0'b);
end;
return ('1'b);
end is_prime; |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Gosu | Gosu |
uses javax.swing.*
uses java.awt.*
uses java.awt.image.*
uses java.awt.event.ActionEvent
uses java.awt.image.BufferedImage#*
uses java.lang.Math#*
var size = 400
EventQueue.invokeLater(\ -> showPlasma())
function showPlasma() {
var frame = new JFrame("Plasma") {:Resizable = false, :DefaultCloseOperation = JFram... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #J | J | require 'trig viewmat'
plasma=: 3 :0
'w h'=. y
X=. (i. % <:) w
Y=. (i. % <:) h
x1=. sin X*16
y1=. sin Y*32
xy1=. sin (Y+/X)*16
xy2=. sin (Y +&.*:/ X)*32
xy1+xy2+y1+/x1
) |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #Haskell | Haskell |
import Control.Monad (guard)
import Data.Array (Array, assocs, elems, listArray, (!))
import Data.Char (toUpper)
import Data.List (nub, (\\))
import Data.List.Split (chunksOf)
import Data.Maybe (listToMaybe)
import Data.String.Utils (replace)
type Square a = Array (Int, Int) a
... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Icon_and_Unicon | Icon and Unicon | link graphics
procedure main() # pinstripe
WOpen("canvas=hidden") # hide for query
height := WAttrib("displayheight") - 45 # adjust for ...
width := WAttrib("displaywidth") - 20 # ... window 7 borders
WClose(&window)
W := WOpen("size="||width||","||height,"bg=black","fg=white... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #PL.2FM | PL/M | 100H: /* TEST FOR PRIMALITY BY TRIAL DIVISION */
DECLARE FALSE LITERALLY '0', TRUE LITERALLY '0FFH';
/* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C );... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Java | Java | import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import static java.awt.image.BufferedImage.*;
import static java.lang.Math.*;
import javax.swing.*;
public class PlasmaEffect extends JPanel {
float[][] plasma;
float hueShift = 0;
BufferedImage img;
public PlasmaEffect() {
... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #J | J | choose=: verb define
sel=. 'Q' e. y
alph=: (sel { 'JQ') -.~ a. {~ 65 + i.26
norm=: [: dedouble alph restrict ('I' I.@:=&'J'@]} ])`(-.&'Q')@.sel@toupper
''
)
restrict=: ] -. -.~
splitDigraph=: ,`([,'X',])@.((= {.) *. 2 | #@])
dedouble=: splitDigraph/&.|. NB. progressively split digraphs i... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #J | J | load'viewmat'
NB. size=. 2{.".wd'qm' NB. J6
NB. size=. getscreenwh_jgtk_ '' NB. J7
size=. 3{".wd'qscreen' NB. J8
'rgb'viewmat- (4<.@%~{:size)# ({.size) $&> 1 2 3 4#&.> <0 1 |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Java | Java | import java.awt.*;
import javax.swing.*;
public class PinstripeDisplay extends JPanel {
final int bands = 4;
public PinstripeDisplay() {
setPreferredSize(new Dimension(900, 600));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int h = ge... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #PowerShell | PowerShell |
function isPrime ($n) {
if ($n -eq 1) {$false}
elseif ($n -eq 2) {$true}
elseif ($n -eq 3) {$true}
else{
$m = [Math]::Floor([Math]::Sqrt($n))
(@(2..$m | where {($_ -lt $n) -and ($n % $_ -eq 0) }).Count -eq 0)
}
}
1..15 | foreach{"isPrime $_ : $(isPrime $_)"} |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #JavaScript | JavaScript | <!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<style>
canvas {
position: absolute;
top: 50%;
left: 50%;
width: 700px;
height: 500px;
margin: -250px 0 0 -350px;
}
body {
background-color: ... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #Java | Java | import java.awt.Point;
import java.util.Scanner;
public class PlayfairCipher {
private static char[][] charTable;
private static Point[] positions;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String key = prompt("Enter an encryption key (min length 6):... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Julia | Julia |
using Luxor
function drawbars(w, h, sections, dk, lt)
Drawing(w,h)
background("white")
width = 1
height = h/sections
for y in 0:height:h-1
setline(width)
for x in 0:w/width
sethue(x % 2 == 0 ? dk: lt)
line(Point(x*width,y), Point(x*width,y+height), :stroke... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Kotlin | Kotlin | // version 1.1.0
import java.awt.*
import javax.swing.*
class ColourPinstripeDisplay(): JPanel() {
private companion object {
val palette = arrayOf(Color.white, Color.black)
}
private val bands = 4
init {
preferredSize = Dimension(900, 600)
}
protected override fun pain... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Prolog | Prolog | prime(2).
prime(N) :-
between(3, inf, N),
1 is N mod 2, % odd
M is floor(sqrt(N+1)), % round-off paranoia
Max is (M-1) // 2, % integer division
forall( between(1, Max, I), N mod (2*I+1) > 0 ). |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Julia | Julia | using Luxor, Colors
Drawing(800, 800)
function plasma(wid, hei)
for x in 1:wid, y in 1:hei
sethue(parse(Colorant, HSV(180 + 45sin(x/19) + 45sin(y/9) +
45sin((x+y)/25) + 45sin(sqrt(x^2 + y^2)/8), 1, 1)))
circle(Point(x, y), 1, :fill)
end
end
@png plasma(800, 800)
|
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Kotlin | Kotlin | // version 1.1.2
import java.awt.*
import java.awt.image.BufferedImage
import javax.swing.*
class PlasmaEffect : JPanel() {
private val plasma: Array<FloatArray>
private var hueShift = 0.0f
private val img: BufferedImage
init {
val dim = Dimension(640, 640)
preferredSize = dim
... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #Julia | Julia | function playfair(key, txt, isencode=true, from = "J", to = "")
to = (to == "" && from == "J") ? "I" : to
function canonical(s, dup_toX=true)
s = replace(replace(uppercase(s), from => to), r"[^A-Z]" => "")
a, dupcount = [c for c in s], 0
for i in 1:2:length(a)-1
if s[i] == ... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Liberty_BASIC | Liberty BASIC |
nomainwin
UpperLeftX=1
UpperLeftY=1
WindowWidth=DisplayWidth
WindowHeight=DisplayHeight
graphicbox #gr.gr, -1, -1, DisplayWidth+4, DisplayHeight+1
open "Pinstripe/Display" for window_popup as #gr
#gr.gr "down"
#gr.gr "trapclose [quit]"
#gr.gr "color black"
#gr.gr "backcolor black"
for w = 1 to 4
y1=y2
... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #PureBasic | PureBasic | Procedure.i IsPrime(n)
Protected k
If n = 2
ProcedureReturn #True
EndIf
If n <= 1 Or n % 2 = 0
ProcedureReturn #False
EndIf
For k = 3 To Int(Sqr(n)) Step 2
If n % k = 0
ProcedureReturn #False
EndIf
Next
ProcedureReturn #True
EndProcedure |
http://rosettacode.org/wiki/Pig_the_dice_game/Player | Pig the dice game/Player | Pig the dice game/Player
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a dice simulator and scorer of Pig the dice game and add to it the ability to play the game to at least one strategy.
State here the play strategies involved.
Show play dur... | #Ada | Ada | with Pig; with Ada.Text_IO; with Ada.Command_Line;
procedure automatic_Pig is
use Pig;
type Robot is new Actor with record
Bound: Natural := 20;
Final_Run: Natural := 0;
end record;
function Roll_More(A: Robot; Self, Opponent: Player'Class) return Boolean;
function Roll_More(A: Robot;... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Lua | Lua |
_ = love.graphics
p1, p2, points = {}, {}, {}
function hypotenuse( a, b )
return a * a + b * b
end
function love.load()
size = _.getWidth()
currentTime, doub, half = 0, size * 2, size / 2
local b1, b2
for j = 0, size * 2 do
for i = 0, size * 2 do
b1 = math.floor( 128 + 127 ... |
http://rosettacode.org/wiki/Playfair_cipher | Playfair cipher | Playfair cipher
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet.
The output of the encrypted and decrypted message ... | #Kotlin | Kotlin | // version 1.0.5-2
enum class PlayfairOption {
NO_Q,
I_EQUALS_J
}
class Playfair(keyword: String, val pfo: PlayfairOption) {
private val table: Array<CharArray> = Array(5, { CharArray(5) }) // 5 x 5 char array
init {
// build table
val used = BooleanArray(26) // all elements fal... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Locomotive_Basic | Locomotive Basic | 10 MODE 2 ' finest resolution
20 sh=400 ' screen height
30 sw=640 ' screen width
40 INK 0,26 ' white ink for background pen (0)
50 INK 1,0 ' black ink for foreground pen (1)
60 FOR sn=1 TO 4 ' four sections
70 bh=INT (sh/4) ' bar height
80 bb=(4-sn)*bh ' bar baseline
90 dw=0 ' currently drawn bar width
100 dc=0 ' cu... |
http://rosettacode.org/wiki/Pinstripe/Display | Pinstripe/Display | Sample image
The task is to demonstrate the creation of a series of vertical pinstripes across the entire width of the display.
in the first quarter the pinstripes should alternate one pixel white, one pixel black = 1 pixel wide vertical pinestripes
Quarter of the way down the display, we can switch to a wider 2 pi... | #Lua | Lua |
function love.load()
WIDTH = love.graphics.getWidth()
ROW_HEIGHT = math.floor(love.graphics.getHeight()/4)
love.graphics.setBackgroundColor({0,0,0})
love.graphics.setLineWidth(1)
love.graphics.setLineStyle("rough")
end
function love.draw()
for j = 0, 3 do
for i = 0, WIDTH, (j+1)*2 d... |
http://rosettacode.org/wiki/Primality_by_trial_division | Primality by trial division | Task
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime.
Use trial division.
Even numbers greater than 2 may be eliminated right away.
A loop from 3 to √ n will suffice, but other loops are allowed.
Related tasks
... | #Python | Python | def prime(a):
return not (a < 2 or any(a % x == 0 for x in xrange(2, int(a**0.5) + 1))) |
http://rosettacode.org/wiki/Pig_the_dice_game/Player | Pig the dice game/Player | Pig the dice game/Player
You are encouraged to solve this task according to the task description, using any language you may know.
Task
Create a dice simulator and scorer of Pig the dice game and add to it the ability to play the game to at least one strategy.
State here the play strategies involved.
Show play dur... | #AutoHotkey | AutoHotkey | #NoEnv
SetBatchLines, -1
#SingleInstance, Force
#Include Pig_the_dice_game_Optimal_Play.ahkl ; comment if you don't want to bother
Play:=10000 ; this is enough to give 2 digits of accuracy in win ratio
Wins0:=Wins1:=0
Player0(TurnSum, SumMe, SumOpp) {
Return practical(TurnSum, SumMe, SumOpp) ; set first player functio... |
http://rosettacode.org/wiki/Plasma_effect | Plasma effect | The plasma effect is a visual effect created by applying various functions, notably sine and cosine, to the color values of screen pixels. When animated (not a task requirement) the effect may give the impression of a colorful flowing liquid.
Task
Create a plasma effect.
See also
Computer Graphics Tutorial (lod... | #Mathematica_.2F_Wolfram_Language | Mathematica / Wolfram Language | s = 400;
Image@Table[
hue = Sin[i/16] + Sin[j/8] + Sin[(i + j)/16] + Sin[Sqrt[i^2 + j^2]/8];
hue = (hue + 4)/8;
Hue[hue, 1, 0.75]
,
{i, 1.0, s},
{j, 1.0, s}
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.