June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,4 +1,5 @@
var doors = falses 100
for a in [:101]:
for b in [a:a:101]: doors[b] = !doors[b]
print "Door $a is $('open.' if doors[a] else 'closed.')"
var doors = falses(100)
for a in 1..100: for b in a..a..100:
doors[b] = not doors[b]
for a in 1..100:
print "Door $a is $((doors[a]) ? 'open.': 'closed.')"

View file

@ -1,22 +1,11 @@
REM "100 Doors" program for QB64 BASIC (http://www.qb64.net/), a QuickBASIC-like compiler.
REM Author: G. A. Tippery
REM Date: 12-Feb-2014
REM
REM Unoptimized (naive) version, per specifications at http://rosettacode.org/wiki/100_doors
DEFINT A-Z
CONST N = 100
DIM door(N)
FOR stride = 1 TO N
FOR index = stride TO N STEP stride
LET door(index) = NOT (door(index))
NEXT index
NEXT stride
PRINT "Open doors:"
FOR index = 1 TO N
IF door(index) THEN PRINT index
NEXT index
END
100 :
110 REM 100 DOORS PROBLEM
120 :
130 DIM D(100)
140 FOR P = 1 TO 100
150 FOR T = P TO 100 STEP P
160 D(T) = NOT D(T): NEXT T
170 NEXT P
180 FOR I = 1 TO 100
190 IF D(I) THEN PRINT I;" ";
200 NEXT I

View file

@ -1,16 +1,34 @@
DIM doors(0 TO 99)
FOR pass = 0 TO 99
FOR door = pass TO 99 STEP pass + 1
PRINT doors(door)
PRINT NOT doors(door)
doors(door) = NOT doors(door)
NEXT door
NEXT pass
FOR i = 0 TO 99
PRINT "Door #"; i + 1; " is ";
IF NOT doors(i) THEN
PRINT "closed"
ELSE
PRINT "open"
END IF
NEXT i
# 100 doors problem
dim d(100)
# simple solution
print "simple solution"
gosub initialize
for t = 1 to 100
for j = t to 100 step t
d[j-1] = not d[j-1]
next j
next t
gosub showopen
# more optimized solution
print "more optimized solution"
gosub initialize
for t = 1 to 10
d[t^2-1] = true
next t
gosub showopen
end
initialize:
for t = 1 to d[?]
d[t-1] = false # closed
next t
return
showopen:
for t = 1 to d[?]
print d[t-1]+ " ";
if t%10 = 0 then print
next t
return

View file

@ -1,12 +1,9 @@
DIM doors(0 TO 99)
FOR door = 0 TO 99
IF INT(SQR(door)) = SQR(door) THEN doors(door) = -1
NEXT door
FOR i = 0 TO 99
PRINT "Door #"; i + 1; " is ";
IF NOT doors(i) THEN
PRINT "closed"
ELSE
PRINT "open"
END IF
NEXT i
10 DIM D(100)
20 FOR I=1 TO 100
30 FOR J=I TO 100 STEP I
40 D(J) = NOT D(J)
50 NEXT J
60 NEXT I
70 FOR I=1 TO 100
80 IF D(I) THEN PRINT I,
90 NEXT I

View file

@ -1,10 +1,22 @@
'lrcvs 04.11.12
cls
x = 1 : y = 3 : z = 0
print x + " Open"
do
z = x + y
print z + " Open"
x = z : y = y + 2
until z >= 100
end
REM "100 Doors" program for QB64 BASIC (http://www.qb64.net/), a QuickBASIC-like compiler.
REM Author: G. A. Tippery
REM Date: 12-Feb-2014
REM
REM Unoptimized (naive) version, per specifications at http://rosettacode.org/wiki/100_doors
DEFINT A-Z
CONST N = 100
DIM door(N)
FOR stride = 1 TO N
FOR index = stride TO N STEP stride
LET door(index) = NOT (door(index))
NEXT index
NEXT stride
PRINT "Open doors:"
FOR index = 1 TO N
IF door(index) THEN PRINT index
NEXT index
END

View file

@ -1,9 +1,16 @@
10 DIM D(100)
20 FOR I=1 TO 100
30 FOR J=I TO 100 STEP I
40 LET D(J)=NOT D(J)
50 NEXT J
60 NEXT I
70 FOR I=1 TO 100
80 IF D(I) THEN PRINT I,
90 NEXT I
DIM doors(0 TO 99)
FOR pass = 0 TO 99
FOR door = pass TO 99 STEP pass + 1
PRINT doors(door)
PRINT NOT doors(door)
doors(door) = NOT doors(door)
NEXT door
NEXT pass
FOR i = 0 TO 99
PRINT "Door #"; i + 1; " is ";
IF NOT doors(i) THEN
PRINT "closed"
ELSE
PRINT "open"
END IF
NEXT i

View file

@ -1,34 +1,12 @@
# 100 doors problem
dim d(100)
# simple solution
print "simple solution"
gosub initialize
for t = 1 to 100
for j = t to 100 step t
d[j-1] = not d[j-1]
next j
next t
gosub showopen
# more optimized solution
print "more optimized solution"
gosub initialize
for t = 1 to 10
d[t^2-1] = true
next t
gosub showopen
end
initialize:
for t = 1 to d[?]
d[t-1] = false # closed
next t
return
showopen:
for t = 1 to d[?]
print d[t-1]+ " ";
if t%10 = 0 then print
next t
return
DIM doors(0 TO 99)
FOR door = 0 TO 99
IF INT(SQR(door)) = SQR(door) THEN doors(door) = -1
NEXT door
FOR i = 0 TO 99
PRINT "Door #"; i + 1; " is ";
IF NOT doors(i) THEN
PRINT "closed"
ELSE
PRINT "open"
END IF
NEXT i

View file

@ -1,9 +1,10 @@
10 DIM D(100)
20 FOR I=1 TO 100
30 FOR J=I TO 100 STEP I
40 D(J) = NOT D(J)
50 NEXT J
60 NEXT I
70 FOR I=1 TO 100
80 IF D(I) THEN PRINT I,
90 NEXT I
'lrcvs 04.11.12
cls
x = 1 : y = 3 : z = 0
print x + " Open"
do
z = x + y
print z + " Open"
x = z : y = y + 2
until z >= 100
end

View file

@ -0,0 +1,9 @@
10 DIM D(100)
20 FOR I=1 TO 100
30 FOR J=I TO 100 STEP I
40 LET D(J)=NOT D(J)
50 NEXT J
60 NEXT I
70 FOR I=1 TO 100
80 IF D(I) THEN PRINT I,
90 NEXT I

View file

@ -7,9 +7,10 @@ namespace ConsoleApplication1
{
//The o variable stores the number of the next OPEN door.
int o = 1;
//The n variable is used to help calculate the next value of the o variable.
int n = 0;
int f = 1;
int l = 5;
Random r = new Random();
o = r.Next(f, l);
//The d variable determines the door to be output next.
for (int d = 1; d <= 100; d++)
@ -18,8 +19,9 @@ namespace ConsoleApplication1
if (d == o)
{
Console.WriteLine("Open");
n++;
o += 2 * n + 1;
f = f + 5;
l = l + 5;
o = r.Next(f, l);
}
else
Console.WriteLine("Closed");

View file

@ -1,10 +1,10 @@
(defun doors (z &optional (w (make-list z)) (n 1))
(if (> n z) w (doors z (toggle w n z) (1+ n))))
(defun toggle (w m z)
(loop for a in w for n from 1 to z
collect (if (zerop (mod n m)) (not a) a)))
(defun doors (z &optional (w (make-list z)) (n 1))
(if (> n z) w (doors z (toggle w n z) (1+ n))))
> (doors 100)
(T NIL NIL T NIL NIL NIL NIL T NIL NIL NIL NIL NIL NIL T NIL NIL NIL NIL NIL
NIL NIL NIL T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL NIL NIL

View file

@ -1,6 +1,10 @@
(defun 100-doors ()
(let ((doors (make-array 100)))
(dotimes (i 10)
(setf (svref doors (* i i)) t))
(dotimes (i 100)
(format t "door ~a: ~:[closed~;open~]~%" (1+ i) (svref doors i)))))
(defun doors (n)
(loop for a from 1 to n collect
(if (zerop (mod (sqrt a) 1)) t nil)))
> (doors 100)
(T NIL NIL T NIL NIL NIL NIL T NIL NIL NIL NIL NIL NIL T NIL NIL NIL NIL NIL
NIL NIL NIL T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL NIL NIL
NIL NIL NIL NIL NIL NIL NIL T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
NIL NIL T NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T
NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL T)

View file

@ -1,23 +1,6 @@
(defun perfect-square-list (n)
"Generates a list of perfect squares from 0 up to n"
(loop for i from 1 to (isqrt n) collect (expt i 2)))
(defun print-doors (doors)
"Pretty prints the doors list"
(format T "~{~A ~A ~A ~A ~A ~A ~A ~A ~A ~A~%~}~%" doors))
(defun open-door (doors num open)
"Sets door at num to open"
(setf (nth (- num 1) doors) open))
(defun visit-all (doors vlist open)
"Visits and opens all the doors indicated in vlist"
(dolist (dn vlist doors)
(open-door doors dn open)))
(defun start2 (&optional (size 100))
"Start the program"
(print-doors
(visit-all (make-list size :initial-element '\#)
(perfect-square-list size)
'_)))
(defun 100-doors ()
(let ((doors (make-array 100)))
(dotimes (i 10)
(setf (svref doors (* i i)) t))
(dotimes (i 100)
(format t "door ~a: ~:[closed~;open~]~%" (1+ i) (svref doors i)))))

View file

@ -1,5 +1,23 @@
(let ((i 0))
(mapcar (lambda (x)
(if (zerop (mod (sqrt (incf i)) 1))
"_" "#"))
(make-list 100)))
(defun perfect-square-list (n)
"Generates a list of perfect squares from 0 up to n"
(loop for i from 1 to (isqrt n) collect (expt i 2)))
(defun print-doors (doors)
"Pretty prints the doors list"
(format T "~{~A ~A ~A ~A ~A ~A ~A ~A ~A ~A~%~}~%" doors))
(defun open-door (doors num open)
"Sets door at num to open"
(setf (nth (- num 1) doors) open))
(defun visit-all (doors vlist open)
"Visits and opens all the doors indicated in vlist"
(dolist (dn vlist doors)
(open-door doors dn open)))
(defun start2 (&optional (size 100))
"Start the program"
(print-doors
(visit-all (make-list size :initial-element '\#)
(perfect-square-list size)
'_)))

View file

@ -0,0 +1,5 @@
(let ((i 0))
(mapcar (lambda (x)
(if (zerop (mod (sqrt (incf i)) 1))
"_" "#"))
(make-list 100)))

View file

@ -1,7 +1,7 @@
import system'routines.
import extensions.
program =
public program =
[
var Doors := Array new:100; populate(:n)( false ).

View file

@ -0,0 +1 @@
=IF($A2/C$1=INT($A2/C$1),IF(B2=0,1,IF(B2=1,0)),B2)

View file

@ -0,0 +1 @@
=IF($A3/C$1=INT($A3/C$1),IF(B3=0,1,IF(B3=1,0)),B3)

View file

@ -0,0 +1 @@
=IF($A2/D$1=INT($A2/D$1),IF(C2=0,1,IF(C2=1,0)),C2)

View file

@ -0,0 +1,14 @@
U8 is_open[100];
U8 pass = 0, door = 0;
/* do the 100 passes */
for (pass = 0; pass < 100; ++pass)
for (door = pass; door < 100; door += pass + 1)
is_open[door] = !is_open[door];
/* output the result */
for (door = 0; door < 100; ++door)
if (is_open[door])
Print("Door #%d is open.\n", door + 1);
else
Print("Door #%d is closed.\n", door + 1);

View file

@ -1,5 +1,5 @@
doors = falses(100)
for a = 1:100, b in a:a:100
for a in 1:100, b in a:a:100
doors[b] = !doors[b]
end
for a = 1:100

View file

@ -1 +1 @@
for i = 1:10 println("Door $(i^2) is open.") end
for i in 1:10 println("Door $(i^2) is open.") end

View file

@ -0,0 +1,2 @@
flip::{,/{(1-*x),1_x}'x:#y}
i::0;(100{i::i+1;flip(i;x)}:*100:^0)?1

View file

@ -0,0 +1 @@
(1+!9)^2

View file

@ -0,0 +1,4 @@
n:=100;reduce xor (count n eachright mod count n eachall<1)
looloooolooooooloooooooolooooooooooloooooooooooolooooooooooooooloooooooooooooooo
looooooooooooooooool

View file

@ -0,0 +1,2 @@
true findall (n:=100;reduce xor (count n eachright mod count n eachall<1))+1
1 4 9 16 25 36 49 64 81 100

View file

@ -0,0 +1,2 @@
count 10 power 2
1 4 9 16 25 36 49 64 81 100

View file

@ -1,7 +1,7 @@
: doors
| i j l |
ListBuffer initValue(100, false) ->l
100 #[ false ] Array init dup ->l
100 loop: i [
i 100 i step: j [ l put(j, l at(j) not) ]
]
l println ;
l . ;

View file

@ -0,0 +1,3 @@
H=100
f=rep(F,H)
which(Reduce(function(d,n) xor(replace(f,seq(n,H,n),T),d), 1:H, f))

View file

@ -1,8 +1,6 @@
#![feature(inclusive_range_syntax)]
fn main() {
let mut door_open = [false; 100];
for pass in 1...100 {
for pass in 1..100 {
let mut door = pass;
while door <= 100 {
door_open[door - 1] = !door_open[door - 1];
@ -13,3 +11,4 @@ fn main() {
println!("Door {} is {}.", i + 1, if is_open {"open"} else {"closed"});
}
}
}

View file

@ -1,8 +1,6 @@
#![feature(inclusive_range_syntax)]
fn main() {
let doors = vec![false; 100].iter_mut().enumerate()
.map(|(door, door_state)| (1...100).into_iter()
.map(|(door, door_state)| (1..100).into_iter()
.filter(|pass| door % pass == 0)
.map(|_| { *door_state = !*door_state; *door_state })
.last().unwrap()).collect::<Vec<_>>();

View file

@ -1,10 +1,8 @@
#![feature(inclusive_range_syntax)]
fn main() {
let squares: Vec<_> = (1...10).map(|n| n*n).collect();
let squares: Vec<_> = (1..10).map(|n| n*n).collect();
let is_square = |num| squares.binary_search(&num).is_ok();
for i in 1...100 {
for i in 1..100 {
let state = if is_square(i) {"open"} else {"closed"};
println!("Door {} is {}", i, state);
}

View file

@ -1,7 +1,5 @@
#![feature(inclusive_range_syntax)]
fn main() {
for i in 1u32...10u32{
for i in 1u32..10u32{
println!("Door {} is open", i.pow(2));
}
}

View file

@ -0,0 +1,43 @@
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! I n i t i a l i z a t i o n
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
doors% = 100
dim doorArray?(doors%)
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! M a i n L o g i c A r e a
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Initialize Array
for index% = 1 to doors%
doorArray?(index%) = false
next index%
// Execute routine
toggle_doors
// Print results
for index% = 1 to doors%
if doorArray?(index%) = true then print index%, ' is open'
next index%
stop
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! R o u t i n e s
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
routine toggle_doors
for index_outer% = 1 to doors%
for index_inner% = 1 to doors%
if mod(index_inner%, index_outer%) = 0 then
doorArray?(index_inner%) = not doorArray?(index_inner%)
end if
next index_inner%
next index_outer%
end routine
end

View file

@ -1,16 +1,16 @@
BEGIN
INTEGER I, PASS;
BOOLEAN ARRAY DOORS(1:100);
INTEGER LIMIT = 100, door, stride;
BOOLEAN ARRAY DOORS(1:LIMIT);
TEXT intro;
FOR I := 1 STEP 1 UNTIL 100 DO
DOORS(I) := FALSE;
FOR PASS := 1 STEP 1 UNTIL 100 DO
FOR I := PASS STEP PASS UNTIL 100 DO
DOORS(I) := NOT DOORS(I);
FOR I := 1 STEP 1 UNTIL 100 DO
IF DOORS(I)
THEN BEGIN OUTTEXT("DOOR "); OUTINT(I,0); OUTTEXT(" IS OPEN"); OUTIMAGE END
FOR stride := 1 STEP 1 UNTIL LIMIT DO
FOR door := stride STEP stride UNTIL LIMIT DO
DOORS(door) := NOT DOORS(door);
intro :- "All doors closed but ";
FOR door := 1 STEP 1 UNTIL LIMIT DO
IF DOORS(door) THEN BEGIN
OUTTEXT(intro); OUTINT(door, 0); intro :- ", "
END;
OUTIMAGE
END.

View file

@ -1,24 +1,22 @@
clear all
clear
set obs 100
gen doors=0
gen index=_n
forvalues i=1/100 {
quietly replace doors=1-doors if mod(_n,`i')==0
}
list index if doors
list index if doors, noobs noheader
+-------+
| index |
|-------|
1. | 1 |
4. | 4 |
9. | 9 |
16. | 16 |
25. | 25 |
|-------|
36. | 36 |
49. | 49 |
64. | 64 |
81. | 81 |
100. | 100 |
+-------+
+-------+
| 1 |
| 4 |
| 9 |
| 16 |
| 25 |
|-------|
| 36 |
| 49 |
| 64 |
| 81 |
| 100 |
+-------+

View file

@ -0,0 +1,17 @@
Public Sub Doors100()
' the state of a door is represented by the data type boolean (false = door closed, true = door opened)
Dim doorstate(1 To 100) As Boolean ' the doorstate()-array is initialized by VB with value 'false'
Dim i As Long, j As Long
For i = 1 To 100
For j = i To 100 Step i
doorstate(j) = Not doorstate(j)
Next j
Next i
Debug.Print "The following doors are open:"
For i = 1 To 100
' print number if door is openend
If doorstate(i) Then Debug.Print CStr(i)
Next i
End Sub