Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/100_doors

View file

@ -0,0 +1,22 @@
There are 100 doors in a row that are all initially closed.
You make 100 [[task feature::Rosetta Code:multiple passes|passes]] by the doors.
The first time through, visit every door and  ''toggle''  the door  (if the door is closed,  open it;   if it is open,  close it).
The second time, only visit every 2<sup>nd</sup> door &nbsp; (door #2, #4, #6, ...), &nbsp; and toggle it.
The third time, visit every 3<sup>rd</sup> door &nbsp; (door #3, #6, #9, ...), etc, &nbsp; until you only visit the 100<sup>th</sup> door.
;Task:
Answer the question: &nbsp; what state are the doors in after the last pass? &nbsp; Which are open, which are closed?
'''[[task feature::Rosetta Code:extra credit|Alternate]]:'''
As noted in this page's &nbsp; [[Talk:100 doors|discussion page]], &nbsp; the only doors that remain open are those whose numbers are perfect squares.
Opening only those doors is an &nbsp; [[task feature::Rosetta Code:optimization|optimization]] &nbsp; that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.
<br><br>

View file

@ -0,0 +1,5 @@
V doors = [0B] * 100
L(i) 100
L(j) (i .< 100).step(i + 1)
doors[j] = !doors[j]
print(Door (i + 1): (I doors[i] {open} E close))

View file

@ -0,0 +1,38 @@
* 100 doors 13/08/2015
HUNDOOR CSECT
USING HUNDOOR,R12
LR R12,R15
LA R6,0
LA R8,1 step 1
LA R9,100
LOOPI BXH R6,R8,ELOOPI do ipass=1 to 100 (R6)
LR R7,R6
SR R7,R6
LR R10,R6 step ipass
LA R11,100
LOOPJ BXH R7,R10,ELOOPJ do idoor=ipass to 100 by ipass (R7)
LA R5,DOORS-1
AR R5,R7
XI 0(R5),X'01' doors(idoor)=not(doors(idoor))
NEXTJ B LOOPJ
ELOOPJ B LOOPI
ELOOPI LA R10,BUFFER R10 address of the buffer
LA R5,DOORS R5 address of doors item
LA R6,1 idoor=1 (R6)
LA R9,100 loop counter
LOOPN CLI 0(R5),X'01' if doors(idoor)=1
BNE NEXTN
XDECO R6,XDEC idoor to decimal
MVC 0(4,R10),XDEC+8 move decimal to buffer
LA R10,4(R10)
NEXTN LA R6,1(R6) idoor=idoor+1
LA R5,1(R5)
BCT R9,LOOPN loop
ELOOPN XPRNT BUFFER,80
RETURN XR R15,R15
BR R14
DOORS DC 100X'00'
BUFFER DC CL80' '
XDEC DS CL12
YREGS
END HUNDOOR

View file

@ -0,0 +1,7 @@
@echo off
set doors=%@repeat[C,100]
do step = 1 to 100
do door = %step to 100 by %step
set doors=%@left[%@eval[%door-1],%doors]%@if[%@instr[%@eval[%door-1],1,%doors]==C,O,C]%@right[%@eval[100-%door],%doors]
enddo
enddo

View file

@ -0,0 +1,3 @@
%@left[n,string] ^: Return n leftmost chars in string
%@right[n,string] ^: Return n rightmost chars in string
%@if[condition,true-val,false-val] ^: Evaluate condition; return true-val if true, false-val if false

View file

@ -0,0 +1,48 @@
; 100 DOORS in 6502 assembly language for: http://www.6502asm.com/beta/index.html
; Written for the original MOS Technology, Inc. NMOS version of the 6502, but should work with any version.
; Based on BASIC QB64 unoptimized version: http://rosettacode.org/wiki/100_doors#BASIC
;
; Notes:
; Doors array[1..100] is at $0201..$0264. On the specified emulator, this is in video memory, so tbe results will
; be directly shown as pixels in the display.
; $0200 (door 0) is cleared for display purposes but is not involved in the open/close loops.
; Y register holds Stride
; X register holds Index
; Zero Page address $01 used to add Stride to Index (via A) because there's no add-to-X or add-Y-to-A instruction.
; First, zero door array
LDA #00
LDX #100
Z_LOOP:
STA 200,X
DEX
BNE Z_LOOP
STA 200,X
; Now do doors repeated open/close
LDY #01 ; Initial value of Stride
S_LOOP:
CPY #101
BCS S_DONE
TYA ; Initial value of Index
I_LOOP:
CMP #101
BCS I_DONE
TAX ; Use as Door array index
INC $200,X ; Toggle bit 0 to reverse state of door
STY 01 ; Add stride (Y) to index (X, via A)
ADC 01
BCC I_LOOP
I_DONE:
INY
BNE S_LOOP
S_DONE:
; Finally, format array values for output: 0 for closed, 1 for open
LDX #100
C_LOOP:
LDA $200,X
AND #$01
STA $200,X
DEX
BNE C_LOOP

View file

@ -0,0 +1,17 @@
;assumes memory at $02xx is initially set to 0 and stack pointer is initialized
;the 1 to 100 door byte array will be at $0200-$0263 (decimal 512 to 611)
;Zero-page location $01 will hold delta
;At end, closed doors = $00, open doors = $01
start: ldx #0 ;initialize index - first door will be at $200 + $0
stx $1
inc $1 ;start out with a delta of 1 (0+1=1)
openloop: inc $200,X ;open X'th door
inc $1 ;add 2 to delta
inc $1
txa ;add delta to X by transferring X to A, adding delta to A, then transferring back to X
clc ; clear carry before adding (6502 has no add-without-carry instruction)
adc $1
tax
cpx #$64 ;check to see if we're at or past the 100th door (at $200 + $63)
bmi openloop ;jump back to openloop if less than 100

View file

@ -0,0 +1,110 @@
*-----------------------------------------------------------
* Title : 100Doors.X68
* Written by : G. A. Tippery
* Date : 2014-01-17
* Description: Solves "100 Doors" problem, see: http://rosettacode.org/wiki/100_doors
* Notes : Translated from C "Unoptimized" version, http://rosettacode.org/wiki/100_doors#unoptimized
* : No optimizations done relative to C version; "for("-equivalent loops could be optimized.
*-----------------------------------------------------------
*
* System-specific general console I/O macros (Sim68K, in this case)
*
PUTS MACRO
** Print a null-terminated string w/o CRLF **
** Usage: PUTS stringaddress
** Returns with D0, A1 modified
MOVEQ #14,D0 ; task number 14 (display null string)
LEA \1,A1 ; address of string
TRAP #15 ; display it
ENDM
*
PRINTN MACRO
** Print decimal integer from number in register
** Usage: PRINTN register
** Returns with D0,D1 modified
IFNC '\1','D1' ;if some register other than D1
MOVE.L \1,D1 ;put number to display in D1
ENDC
MOVE.B #3,D0
TRAP #15 ;display number in D1
*
* Generic constants
*
CR EQU 13 ;ASCII Carriage Return
LF EQU 10 ;ASCII Line Feed
*
* Definitions specific to this program
*
* Register usage:
* D3 == pass (index)
* D4 == door (index)
* A2 == Doors array pointer
*
SIZE EQU 100 ;Define a symbolic constant for # of doors
ORG $1000 ;Specify load address for program -- actual address system-specific
START: ; Execution starts here
LEA Doors,A2 ; make A2 point to Doors byte array
MOVEQ #0,D3
PassLoop:
CMP #SIZE,D3
BCC ExitPassLoop ; Branch on Carry Clear - being used as Branch on Higher or Equal
MOVE D3,D4
DoorLoop:
CMP #SIZE,D4
BCC ExitDoorLoop
NOT.B 0(A2,D4)
ADD D3,D4
ADDQ #1,D4
BRA DoorLoop
ExitDoorLoop:
ADDQ #1,D3
BRA PassLoop
ExitPassLoop:
* $28 = 40. bytes of code to this point. 32626 cycles so far.
* At this point, the result exists as the 100 bytes starting at address Doors.
* To get output, we must use methods specific to the particular hardware, OS, or
* emulator system that the code is running on. I use macros to "hide" some of the
* system-specific details; equivalent macros would be written for another system.
MOVEQ #0,D4
PrintLoop:
CMP #SIZE,D4
BCC ExitPrintLoop
PUTS DoorMsg1
MOVE D4,D1
ADDQ #1,D1 ; Convert index to 1-based instead of 0-based
PRINTN D1
PUTS DoorMsg2
TST.B 0(A2,D4) ; Is this door open (!= 0)?
BNE ItsOpen
PUTS DoorMsgC
BRA Next
ItsOpen:
PUTS DoorMsgO
Next:
ADDQ #1,D4
BRA PrintLoop
ExitPrintLoop:
* What to do at end of program is also system-specific
SIMHALT ;Halt simulator
*
* $78 = 120. bytes of code to this point, but this will depend on how the I/O macros are actually written.
* Cycle count is nearly meaningless, as the I/O hardware and routines will dominate the timing.
*
* Data memory usage
*
ORG $2000
Doors DCB.B SIZE,0 ;Reserve 100 bytes, prefilled with zeros
DoorMsg1 DC.B 'Door ',0
DoorMsg2 DC.B ' is ',0
DoorMsgC DC.B 'closed',CR,LF,0
DoorMsgO DC.B 'open',CR,LF,0
END START ;last line of source

View file

@ -0,0 +1,37 @@
page: equ 2 ; Store doors in page 2
doors: equ 100 ; 100 doors
puts: equ 9 ; CP/M string output
org 100h
xra a ; Set all doors to zero
lxi h,256*page
mvi c,doors
zero: mov m,a
inx h
dcr c
jnz zero
mvi m,'$' ; CP/M string terminator (for easier output later)
mov d,a ; D=0 so that DE=E=pass counter
mov e,a ; E=0, first pass
mvi a,doors-1 ; Last pass and door
pass: mov l,e ; L=door counter, start at first door in pass
door: inr m ; Incrementing always toggles the low bit
dad d ; Go to next door in pass
inr l
cmp l ; Was this the last door?
jnc door ; If not, do the next door
inr e ; Next pass
cmp e ; Was this the last pass?
jnc pass ; If not, do the next pass
lxi h,256*page
mvi c,doors ; Door counter
lxi d,130h ; D=1 (low bit), E=30h (ascii 0)
char: mov a,m ; Get door
ana d ; Low bit gives door status
ora e ; ASCII 0 or 1
mov m,a ; Write character back
inx h ; Next door
dcr c ; Any doors left?
jnz char ; If so, next door
lxi d,256*page
mvi c,puts ; CP/M system call to print the string
jmp 5

View file

@ -0,0 +1,31 @@
\ Array of doors; init to empty; accessing a non-extant member will return
\ 'null', which is treated as 'false', so we don't need to initialize it:
[] var, doors
\ given a door number, get the value and toggle it:
: toggle-door \ n --
doors @ over a:@
not rot swap a:! drop ;
\ print which doors are open:
: .doors
(
doors @ over a:@ nip
if . space else drop then
) 1 100 loop ;
\ iterate over the doors, skipping 'n':
: main-pass \ n --
0
true
repeat
drop
dup toggle-door
over n:+
dup 101 <
while 2drop drop ;
\ calculate the first 100 doors:
' main-pass 1 100 loop
\ print the results:
.doors cr bye

View file

@ -0,0 +1,79 @@
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program 100doors64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ NBDOORS, 100
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz "The door @ is open.\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
stTableDoors: .skip 8 * NBDOORS
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
// display first line
ldr x3,qAdrstTableDoors // table address
mov x5,1
1:
mov x4,x5
2: // begin loop
ldr x2,[x3,x4,lsl #3] // read doors index x4
cmp x2,#0
cset x2,eq
//moveq x2,#1 // if x2 = 0 1 -> x2
//movne x2,#0 // if x2 = 1 0 -> x2
str x2,[x3,x4,lsl #3] // store value of doors
add x4,x4,x5 // increment x4 with x5 value
cmp x4,NBDOORS // number of doors ?
ble 2b // no -> loop
add x5,x5,#1 // increment the increment !!
cmp x5,NBDOORS // number of doors ?
ble 1b // no -> loop
// loop display state doors
mov x4,#0
3:
ldr x2,[x3,x4,lsl #3] // read state doors x4 index
cmp x2,#0
beq 4f
mov x0,x4 // open -> display message
ldr x1,qAdrsZoneConv // display value index
bl conversion10 // call function
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at first @ character
bl affichageMess // display message
4:
add x4,x4,1
cmp x4,NBDOORS
ble 3b // loop
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrstTableDoors: .quad stTableDoors
qAdrsMessResult: .quad sMessResult
qAdrsZoneConv: .quad sZoneConv
/***********************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

View file

@ -0,0 +1,56 @@
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program 100doors64_1.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ NBDOORS, 100
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz "The door @ is open.\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x5,3
mov x4,1
1:
mov x0,x4
ldr x1,qAdrsZoneConv // display value index
bl conversion10 // call function
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at first @ character
bl affichageMess // display message
add x4,x4,x5
add x5,x5,2
cmp x4,NBDOORS
ble 1b // loop
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsMessResult: .quad sMessResult
qAdrsZoneConv: .quad sZoneConv
/***********************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

View file

@ -0,0 +1,30 @@
form open_doors_unopt.
data: lv_door type i,
lv_count type i value 1.
data: lt_doors type standard table of c initial size 100.
field-symbols: <wa_door> type c.
do 100 times.
append initial line to lt_doors assigning <wa_door>.
<wa_door> = 'X'.
enddo.
while lv_count < 100.
lv_count = lv_count + 1.
lv_door = lv_count.
while lv_door < 100.
read table lt_doors index lv_door assigning <wa_door>.
if <wa_door> = ' '.
<wa_door> = 'X'.
else.
<wa_door> = ' '.
endif.
add lv_count to lv_door.
endwhile.
endwhile.
loop at lt_doors assigning <wa_door>.
if <wa_door> = 'X'.
write : / 'Door', (4) sy-tabix right-justified, 'is open' no-gap.
endif.
endloop.
endform.

View file

@ -0,0 +1,9 @@
cl_demo_output=>display( REDUCE stringtab( INIT list TYPE stringtab
aux TYPE i
FOR door = 1 WHILE door <= 100
FOR pass = 1 WHILE pass <= 100
NEXT aux = COND #( WHEN pass = 1 THEN 1
WHEN door MOD pass = 0 THEN aux + 1 ELSE aux )
list = COND #( WHEN pass = 100
THEN COND #( WHEN aux MOD 2 <> 0 THEN VALUE #( BASE list ( CONV #( door ) ) )
ELSE list ) ELSE list ) ) ).

View file

@ -0,0 +1,14 @@
form open_doors_opt.
data: lv_square type i value 1,
lv_inc type i value 3.
data: lt_doors type standard table of c initial size 100.
field-symbols: <wa_door> type c.
do 100 times.
append initial line to lt_doors assigning <wa_door>.
if sy-index = lv_square.
<wa_door> = 'X'.
add: lv_inc to lv_square, 2 to lv_inc.
write : / 'Door', (4) sy-index right-justified, 'is open' no-gap.
endif.
enddo.
endform.

View file

@ -0,0 +1,4 @@
DO 10 TIMES.
DATA(val) = sy-index * sy-index.
WRITE: / val.
ENDDO.

View file

@ -0,0 +1,3 @@
cl_demo_output=>display( REDUCE stringtab( INIT list TYPE stringtab
FOR i = 1 WHILE i <= 10
NEXT list = VALUE #( BASE list ( i * i ) ) ) ).

View file

@ -0,0 +1,21 @@
(defun rep (n x)
(if (zp n)
nil
(cons x
(rep (- n 1) x))))
(defun toggle-every-r (n i bs)
(if (endp bs)
nil
(cons (if (zp i)
(not (first bs))
(first bs))
(toggle-every-r n (mod (1- i) n) (rest bs)))))
(defun toggle-every (n bs)
(toggle-every-r n (1- n) bs))
(defun 100-doors (i doors)
(if (zp i)
doors
(100-doors (1- i) (toggle-every i doors))))

View file

@ -0,0 +1,31 @@
begin
comment - 100 doors problem in ALGOL-60;
boolean array doors[1:100];
integer i, j;
boolean open, closed;
open := true;
closed := not true;
outstring(1,"100 Doors Problem\n");
comment - all doors are initially closed;
for i := 1 step 1 until 100 do
doors[i] := closed;
comment
cycle through at increasing intervals
and flip each door encountered;
for i := 1 step 1 until 100 do
for j := i step i until 100 do
doors[j] := not doors[j];
comment - show which doors are open;
outstring(1,"The open doors are:");
for i := 1 step 1 until 100 do
if doors[i] then
outinteger(1,i);
end

View file

@ -0,0 +1,25 @@
# declare some constants #
INT limit = 100;
PROC doors = VOID:
(
MODE DOORSTATE = BOOL;
BOOL closed = FALSE;
BOOL open = NOT closed;
MODE DOORLIST = [limit]DOORSTATE;
DOORLIST the doors;
FOR i FROM LWB the doors TO UPB the doors DO the doors[i]:=closed OD;
FOR i FROM LWB the doors TO UPB the doors DO
FOR j FROM LWB the doors TO UPB the doors DO
IF j MOD i = 0 THEN
the doors[j] := NOT the doors[j]
FI
OD
OD;
FOR i FROM LWB the doors TO UPB the doors DO
printf(($g" is "gl$,i,(the doors[i]|"opened"|"closed")))
OD
);
doors;

View file

@ -0,0 +1,7 @@
PROC doors optimised = ( INT limit )VOID:
FOR i TO limit DO
REAL num := sqrt(i);
printf(($g" is "gl$,i,(ENTIER num = num |"opened"|"closed") ))
OD
;
doors optimised(limit)

View file

@ -0,0 +1,33 @@
BEGIN
INTEGER ARRAY DOORS[1:100];
INTEGER I, J, OPEN, CLOSED;
OPEN := 1;
CLOSED := 0;
% ALL DOORS ARE INITIALLY CLOSED %
FOR I := 1 STEP 1 UNTIL 100 DO
BEGIN
DOORS[I] := CLOSED;
END;
% PASS THROUGH AT INCREASING INTERVALS AND FLIP %
FOR I := 1 STEP 1 UNTIL 100 DO
BEGIN
FOR J := I STEP I UNTIL 100 DO
BEGIN
DOORS[J] := 1 - DOORS[J];
END;
END;
% SHOW RESULTS %
WRITE("THE OPEN DOORS ARE:");
WRITE("");
FOR I := 1 STEP 1 UNTIL 100 DO
BEGIN
IF DOORS[I] = OPEN THEN
WRITEON(I);
END;
END

View file

@ -0,0 +1,32 @@
begin
% -- find the first few squares via the unoptimised door flipping method %
integer doorMax;
doorMax := 100;
begin
% -- need to start a new block so the array can have variable bounds %
% -- array of doors - door( i ) is true if open, false if closed %
logical array door( 1 :: doorMax );
% -- set all doors to closed %
for i := 1 until doorMax do door( i ) := false;
% -- repeatedly flip the doors %
for i := 1 until doorMax
do begin
for j := i step i until doorMax
do begin
door( j ) := not door( j )
end
end;
% -- display the results %
i_w := 1; % -- set integer field width %
s_w := 1; % -- and separator width %
for i := 1 until doorMax do if door( i ) then writeon( i )
end
end.

View file

@ -0,0 +1,2 @@
doors{100((-1)0),1}
doors¨ 100

View file

@ -0,0 +1,3 @@
⍝⍝ Also works with GNU APL after introduction of
⍝⍝ the ⍸ function with SVN r1368, Dec 03 2020
0=(100).|100

View file

@ -0,0 +1,154 @@
/* ARM assembly Raspberry PI */
/* program 100doors.s */
/************************************/
/* Constantes */
/************************************/
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ NBDOORS, 100
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .ascii "The door "
sMessValeur: .fill 11, 1, ' ' @ size => 11
.asciz "is open.\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
stTableDoors: .skip 4 * NBDOORS
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
push {fp,lr} @ saves 2 registers
@ display first line
ldr r3,iAdrstTableDoors @ table address
mov r5,#1
1:
mov r4,r5
2: @ begin loop
ldr r2,[r3,r4,lsl #2] @ read doors index r4
cmp r2,#0
moveq r2,#1 @ if r2 = 0 1 -> r2
movne r2,#0 @ if r2 = 1 0 -> r2
str r2,[r3,r4,lsl #2] @ store value of doors
add r4,r5 @ increment r4 with r5 value
cmp r4,#NBDOORS @ number of doors ?
ble 2b @ no -> loop
add r5,#1 @ increment the increment !!
cmp r5,#NBDOORS @ number of doors ?
ble 1b @ no -> loop
@ loop display state doors
mov r4,#0
3:
ldr r2,[r3,r4,lsl #2] @ read state doors r4 index
cmp r2,#0
beq 4f
mov r0,r4 @ open -> display message
ldr r1,iAdrsMessValeur @ display value index
bl conversion10 @ call function
ldr r0,iAdrsMessResult
bl affichageMess @ display message
4:
add r4,#1
cmp r4,#NBDOORS
ble 3b @ loop
100: @ standard end of the program
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrsMessValeur: .int sMessValeur
iAdrstTableDoors: .int stTableDoors
iAdrsMessResult: .int sMessResult
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
/******************************************************************/
/* Converting a register to a decimal unsigned */
/******************************************************************/
/* r0 contains value and r1 address area */
/* r0 return size of result (no zero final in area) */
/* area size => 11 bytes */
.equ LGZONECAL, 10
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#LGZONECAL
1: @ start loop
bl divisionpar10U @unsigned r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
cmp r0,#0 @ stop if quotient = 0
subne r2,#1 @ else previous position
bne 1b @ and loop
// and move digit from left of area
mov r4,#0
2:
ldrb r1,[r3,r2]
strb r1,[r3,r4]
add r2,#1
add r4,#1
cmp r2,#LGZONECAL
ble 2b
// and move spaces in end on area
mov r0,r4 @ result length
mov r1,#' ' @ space
3:
strb r1,[r3,r4] @ store space in area
add r4,#1 @ next position
cmp r4,#LGZONECAL
ble 3b @ loop if r4 <= area size
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 unsigned */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10U:
push {r2,r3,r4, lr}
mov r4,r0 @ save value
//mov r3,#0xCCCD @ r3 <- magic_number lower @ for Raspberry pi 3
//movt r3,#0xCCCC @ r3 <- magic_number upper @ for Raspberry pi 3
ldr r3,iMagicNumber @ for Raspberry pi 1 2
umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0)
mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
pop {r2,r3,r4,lr}
bx lr @ leave function
iMagicNumber: .int 0xCCCCCCCD

View file

@ -0,0 +1,136 @@
/*********************************************/
/* optimized version */
/*********************************************/
/* ARM assembly Raspberry PI */
/* program 100doors.s */
/************************************/
/* Constantes */
/************************************/
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ NBDOORS, 100
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .ascii "The door "
sMessValeur: .fill 11, 1, ' ' @ size => 11
.asciz "is open.\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
push {fp,lr} @ saves 2 registers
@ display first line
mov r5,#3 @ start value of increment
mov r4,#1 @ start doors
@ loop display state doors
1:
mov r0,r4 @ open -> display message
ldr r1,iAdrsMessValeur @ display value index
bl conversion10 @ call function
ldr r0,iAdrsMessResult
bl affichageMess @ display message
add r4,r5 @ add increment
add r5,#2 @ new increment
cmp r4,#NBDOORS
ble 1b @ loop
100: @ standard end of the program
mov r0, #0 @ return code
pop {fp,lr} @ restaur 2 registers
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrsMessValeur: .int sMessValeur
iAdrsMessResult: .int sMessResult
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
/******************************************************************/
/* Converting a register to a decimal unsigned */
/******************************************************************/
/* r0 contains value and r1 address area */
/* r0 return size of result (no zero final in area) */
/* area size => 11 bytes */
.equ LGZONECAL, 10
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#LGZONECAL
1: @ start loop
bl divisionpar10U @ unsigned r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
cmp r0,#0 @ stop if quotient = 0
subne r2,#1 @ else previous position
bne 1b @ and loop
@ and move digit from left of area
mov r4,#0
2:
ldrb r1,[r3,r2]
strb r1,[r3,r4]
add r2,#1
add r4,#1
cmp r2,#LGZONECAL
ble 2b
@ and move spaces in end on area
mov r0,r4 @ result length
mov r1,#' ' @ space
3:
strb r1,[r3,r4] @ store space in area
add r4,#1 @ next position
cmp r4,#LGZONECAL
ble 3b @ loop if r4 <= area size
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 unsigned */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10U:
push {r2,r3,r4, lr}
mov r4,r0 @ save value
//mov r3,#0xCCCD @ r3 <- magic_number lower @ for raspberry 3
//movt r3,#0xCCCC @ r3 <- magic_number upper @ for raspberry 3
ldr r3,iMagicNumber @ for raspberry 1 2
umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0)
mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
pop {r2,r3,r4,lr}
bx lr @ leave function
iMagicNumber: .int 0xCCCCCCCD

View file

@ -0,0 +1,41 @@
#include "share/atspre_staload.hats"
implement
main0((*void*)) = let
//
var A = @[bool][100](false)
val A = $UNSAFE.cast{arrayref(bool,100)}(addr@A)
//
fnx
loop
(
pass: intGte(0)
) : void =
if pass < 100
then loop2 (pass, pass)
// end of [if]
and
loop2
(
pass: natLt(100), door: intGte(0)
) : void =
if door < 100
then (A[door] := ~A[door]; loop2(pass, door+pass+1))
else loop(pass+1)
// end of [if]
//
fun
loop3
(
door: intGte(0)
) : void =
if door < 100
then (
println!("door #", door+1, " is ", (if A[door] then "open" else "closed"): string, ".");
loop3(door+1)
) (* end of [then] *)
// end of [if]
//
in
loop(0); loop3 (0)
end // end of [main0]

View file

@ -0,0 +1,17 @@
BEGIN {
for(i=1; i <= 100; i++)
{
doors[i] = 0 # close the doors
}
for(i=1; i <= 100; i++)
{
for(j=i; j <= 100; j += i)
{
doors[j] = (doors[j]+1) % 2
}
}
for(i=1; i <= 100; i++)
{
print i, doors[i] ? "open" : "close"
}
}

View file

@ -0,0 +1,14 @@
BEGIN {
for(i=1; i <= 100; i++) {
doors[i] = 0 # close the doors
}
for(i=1; i <= 100; i++) {
if ( int(sqrt(i)) == sqrt(i) ) {
doors[i] = 1
}
}
for(i=1; i <= 100; i++)
{
print i, doors[i] ? "open" : "close"
}
}

View file

@ -0,0 +1,23 @@
DEFINE COUNT="100"
PROC Main()
BYTE ARRAY doors(COUNT+1)
BYTE door,pass
FOR door=1 TO COUNT
DO
doors(door)=0
OD
PrintE("Following doors are open:")
FOR pass=1 TO COUNT
DO
FOR door=pass TO COUNT STEP pass
DO
doors(door)==!$FF
OD
IF doors(pass)=$FF THEN
PrintB(pass) Put(32)
FI
OD
RETURN

View file

@ -0,0 +1,21 @@
package {
import flash.display.Sprite;
public class Doors extends Sprite {
public function Doors() {
// Initialize the array
var doors:Array = new Array(100);
for (var i:Number = 0; i < 100; i++) {
doors[i] = false;
// Do the work
for (var pass:Number = 0; pass < 100; pass++) {
for (var j:Number = pass; j < 100; j += (pass+1)) {
doors[j] = !doors[j];
}
}
trace(doors);
}
}
}

View file

@ -0,0 +1,36 @@
VAR sStatus: SHORT
VAR sArray: SHORT
VAR sCount: SHORT
VAR sDoor: SHORT
VAR sPass: SHORT
VAR zIndex: STRING
VAR zState: STRING
//
SET sStatus = GET_UNUSED_ARRAY_HANDLE(sArray)
SET sStatus = INIT_SORTED_ARRAY(sArray, 0, 0, 1)
//
DO sCount = 1 TO 100
DO sPass = 1 TO 100
SET sDoor = sCount * sPass
IF sDoor <= 100
SET zIndex = REPEAT("0", 3 - LENGTH(STR(sDoor))) + STR(sDoor)
SET sStatus = READ_ARRAY_REC("=", sArray, zIndex)
SET zState = "OPEN"
IF GET_STRING_SAY(sArray, 1) = "OPEN"
SET zState = "CLOSE"
ENDIF
//
SET sStatus = ADD_ARRAY_REC(sArray, zIndex)
SET sStatus = PUT_STRING_SAY(sArray, 1, zState)
ELSE
BREAK
ENDIF
ENDDO
ENDDO
//
SET zIndex = ""
SET sStatus = READ_ARRAY_REC(">=", sArray, zIndex)
DO WHILE sStatus = 0
>>Door: ^zIndex^ State: ^GET_STRING_SAY(sArray, 1)^
SET sStatus = READ_ARRAY_REC("+", sArray, zIndex)
ENDDO

View file

@ -0,0 +1,22 @@
with Ada.Text_Io; use Ada.Text_Io;
procedure Doors is
type Door_State is (Closed, Open);
type Door_List is array(Positive range 1..100) of Door_State;
The_Doors : Door_List := (others => Closed);
begin
for I in 1..100 loop
for J in The_Doors'range loop
if J mod I = 0 then
if The_Doors(J) = Closed then
The_Doors(J) := Open;
else
The_Doors(J) := Closed;
end if;
end if;
end loop;
end loop;
for I in The_Doors'range loop
Put_Line(Integer'Image(I) & " is " & Door_State'Image(The_Doors(I)));
end loop;
end Doors;

View file

@ -0,0 +1,16 @@
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
procedure Doors_Optimized is
Num : Float;
begin
for I in 1..100 loop
Num := Sqrt(Float(I));
Put(Integer'Image(I) & " is ");
if Float'Floor(Num) = Num then
Put_Line("Opened");
else
Put_Line("Closed");
end if;
end loop;
end Doors_Optimized;

View file

@ -0,0 +1,19 @@
# find the first few squares via the unoptimised door flipping method
scope
local doorMax := 100;
local door;
create register door( doorMax );
# set all doors to closed
for i to doorMax do door[ i ] := false od;
# repeatedly flip the doors
for i to doorMax do
for j from i to doorMax by i do door[ j ] := not door[ j ] od
od;
# display the results
for i to doorMax do if door[ i ] then write( " ", i ) fi od; print()
epocs

View file

@ -0,0 +1,13 @@
var doors = new int [100]
foreach pass 100 {
for (var door = pass ; door < 100 ; door += pass+1) {
doors[door] = !doors[door]
}
}
var d = 1
foreach door doors {
println ("door " + d++ + " is " + (door ? "open" : "closed"))
}

View file

@ -0,0 +1,14 @@
PROC main()
DEF t[100]: ARRAY,
pass, door
FOR door := 0 TO 99 DO t[door] := FALSE
FOR pass := 0 TO 99
door := pass
WHILE door <= 99
t[door] := Not(t[door])
door := door + pass + 1
ENDWHILE
ENDFOR
FOR door := 0 TO 99 DO WriteF('\d is \s\n', door+1,
IF t[door] THEN 'open' ELSE 'closed')
ENDPROC

View file

@ -0,0 +1,17 @@
set is_open to {}
repeat 100 times
set end of is_open to false
end
repeat with pass from 1 to 100
repeat with door from pass to 100 by pass
set item door of is_open to not item door of is_open
end
end
set open_doors to {}
repeat with door from 1 to 100
if item door of is_open then
set end of open_doors to door
end
end
set text item delimiters to ", "
display dialog "Open doors: " & open_doors

View file

@ -0,0 +1,27 @@
on _100doors()
script o
property doors : {}
end script
repeat 100 times
set end of o's doors to false -- false = "not open".
end repeat
repeat with pass from 1 to 100
if (not item pass of o's doors) then set item pass of o's doors to pass
repeat with d from (pass + pass) to 100 by pass
set item d of o's doors to (not item d of o's doors)
end repeat
end repeat
return o's doors's integers
end _100doors
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
return "Open doors:
" & join(_100doors(), ", ")

View file

@ -0,0 +1,2 @@
"Open doors:
1, 4, 9, 16, 25, 36, 49, 64, 81, 100"

View file

@ -0,0 +1,149 @@
-- FINAL DOOR STATES ---------------------------------------------------------
-- finalDoors :: Int -> [(Int, Bool)]
on finalDoors(n)
-- toggledCorridor :: [(Int, Bool)] -> (Int, Bool) -> Int -> [(Int, Bool)]
script toggledCorridor
on |λ|(a, _, k)
-- perhapsToggled :: Bool -> Int -> Bool
script perhapsToggled
on |λ|(x, i)
if i mod k = 0 then
{i, not item 2 of x}
else
{i, item 2 of x}
end if
end |λ|
end script
map(perhapsToggled, a)
end |λ|
end script
set xs to enumFromTo(1, n)
foldl(toggledCorridor, ¬
zip(xs, replicate(n, {false})), xs)
end finalDoors
-- TEST ----------------------------------------------------------------------
on run
-- isOpenAtEnd :: (Int, Bool) -> Bool
script isOpenAtEnd
on |λ|(door)
(item 2 of door)
end |λ|
end script
-- doorNumber :: (Int, Bool) -> Int
script doorNumber
on |λ|(door)
(item 1 of door)
end |λ|
end script
map(doorNumber, filter(isOpenAtEnd, finalDoors(100)))
--> {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- min :: Ord a => a -> a -> a
on min(x, y)
if y < x then
y
else
x
end if
end min
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- replicate :: Int -> a -> [a]
on replicate(n, a)
set out to {}
if n < 1 then return out
set dbl to {a}
repeat while (n > 1)
if (n mod 2) > 0 then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicate
-- zip :: [a] -> [b] -> [(a, b)]
on zip(xs, ys)
set lng to min(length of xs, length of ys)
set lst to {}
repeat with i from 1 to lng
set end of lst to {item i of xs, item i of ys}
end repeat
return lst
end zip

View file

@ -0,0 +1 @@
{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

View file

@ -0,0 +1,5 @@
map(factorCountMod2, enumFromTo(1, 100))
on factorCountMod2(n)
{n, (length of integerFactors(n)) mod 2 = 1}
end factorCountMod2

View file

@ -0,0 +1,21 @@
-- perfectSquaresUpTo :: Int -> [Int]
on perfectSquaresUpTo(n)
script squared
-- (Int -> Int)
on |λ|(x)
x * x
end |λ|
end script
set realRoot to n ^ (1 / 2)
set intRoot to realRoot as integer
set blnNotPerfectSquare to not (intRoot = realRoot)
map(squared, enumFromTo(1, intRoot - (blnNotPerfectSquare as integer)))
end perfectSquaresUpTo
on run
perfectSquaresUpTo(100)
end run

View file

@ -0,0 +1 @@
{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

View file

@ -0,0 +1,11 @@
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

@ -0,0 +1,12 @@
openshut(n):
for x in [1..n]
x%n==0
pass(n):
if n==100
openshut(n)
else
openshut(n) xor pass(n+1)
100doors():
pass(1) -> io

View file

@ -0,0 +1,16 @@
use std, array
close all doors
for each pass from 1 to 100
for (door = pass) (door <= 100) (door += pass)
toggle door
let int pass, door.
.: close all doors :. {memset doors 0 size of doors}
.:toggle <int door>:. { !!(doors[door - 1]) }
let doors be an array of 100 bool
for each door from 1 to 100
printf "#%.3d %s\n" door (doors[door - 1]) ? "[ ]", "[X]"

View file

@ -0,0 +1,11 @@
isOpen: map 1..101 => false
loop 1..100 'pass ->
loop (range.step:pass pass 100) 'door [
isOpen\[door]: not? isOpen\[door]
]
loop 1..100 'x ->
if isOpen\[x] [
print ["Door" x "is open."]
]

View file

@ -0,0 +1,7 @@
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

@ -0,0 +1,6 @@
for(int i = 1; i < 100; ++i) {
if (i % i^2 < 11) {
write("Door ", i^2, suffix=none);
write(" is open");
}
}

View file

@ -0,0 +1,27 @@
Loop, 100
Door%A_Index% := "closed"
Loop, 100 {
x := A_Index, y := A_Index
While (x <= 100)
{
CurrentDoor := Door%x%
If CurrentDoor contains closed
{
Door%x% := "open"
x += y
}
else if CurrentDoor contains open
{
Door%x% := "closed"
x += y
}
}
}
Loop, 100 {
CurrentDoor := Door%A_Index%
If CurrentDoor contains open
Res .= "Door " A_Index " is open`n"
}
MsgBox % Res

View file

@ -0,0 +1,6 @@
increment := 3, square := 1
Loop, 100
If (A_Index = square)
outstring .= "`nDoor " A_Index " is open"
,square += increment, increment += 2
MsgBox,, Succesfull, % SubStr(outstring, 2)

View file

@ -0,0 +1,3 @@
While (Door := A_Index ** 2) <= 100
Result .= "Door " Door " is open`n"
MsgBox, %Result%

View file

@ -0,0 +1,17 @@
#include <array.au3>
$doors = 100
;door array, 0 = closed, 1 = open
Local $door[$doors +1]
For $ii = 1 To $doors
For $i = $ii To $doors Step $ii
$door[$i] = Not $door[$i]
next
Next
;display to screen
For $i = 1 To $doors
ConsoleWrite (Number($door[$i])& " ")
If Mod($i,10) = 0 Then ConsoleWrite(@CRLF)
Next

View file

@ -0,0 +1,6 @@
(open,closed,change,open?) := (true,false,not,test);
doors := bits(100,closed);
for i in 1..#doors repeat
for j in i..#doors by i repeat
doors.j := change doors.j
[i for i in 1..#doors | open? doors.i]

View file

@ -0,0 +1,2 @@
[i for i in 1..100 | perfectSquare? i] -- or
[i^2 for i in 1..sqrt(100)::Integer]

View file

@ -0,0 +1,29 @@
main()
{
auto doors[100]; /* != 0 means open */
auto pass, door;
door = 0;
while( door<100 ) doors[door++] = 0;
pass = 0;
while( pass<100 )
{
door = pass;
while( door<100 )
{
doors[door] = !doors[door];
door =+ pass+1;
}
++pass;
}
door = 0;
while( door<100 )
{
printf("door #%d is %s.*n", door+1, doors[door] ? "open" : "closed");
++door;
}
return(0);
}

View file

@ -0,0 +1,34 @@
# 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

@ -0,0 +1 @@
for i = 1 to 10 : if i % i^2 < 11 then print "La puerta "; int(i^2); " esta abierta" : end if : next i : end

View file

@ -0,0 +1,9 @@
DIM doors%(100)
FOR pass% = 1 TO 100
FOR door% = pass% TO 100 STEP pass%
doors%(door%) = NOT doors%(door%)
NEXT door%
NEXT pass%
FOR door% = 1 TO 100
IF doors%(door%) PRINT "Door " ; door% " is open"
NEXT door%

View file

@ -0,0 +1,22 @@
get "libhdr"
let start() be
$( let doors = vec 100
// close all doors
for n = 1 to 100 do doors!n := 0
// make 100 passes
for pass = 1 to 100 do
$( let n = pass
while n <= 100 do
$( doors!n := ~doors!n
n := n + pass
$)
$)
// report which doors are open
for n = 1 to 100 do
if doors!n then
writef("Door %N is open.*N", n)
$)

View file

@ -0,0 +1,2 @@
swch ´{1001«𝕩0}¨1+100
¯1{𝕩@+10}¨•Fmt¨swch,/swch

View file

@ -0,0 +1,2 @@
" 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 3 8 15 24 35 48 63 80 99 "

View file

@ -0,0 +1 @@
swch

View file

@ -0,0 +1,13 @@
OPTION BASE 1
DECLARE doors[100]
FOR size = 1 TO 100
FOR pass = 0 TO 100 STEP size
doors[pass] = NOT(doors[pass])
NEXT
NEXT
FOR which = 1 TO 100
IF doors[which] THEN PRINT which
NEXT

View file

@ -0,0 +1,22 @@
const NUM_DOORS := 100
fun main() {
// All doors are closed by default (`false`)
mut is_open := []bool{length = NUM_DOORS}
// Make 100 passes by the doors
for pass := 0; pass < 100; pass += 1 {
// Only visit every `pass + 1`th door
for door := pass; door < NUM_DOORS; door += pass + 1 {
is_open[door] = not is_open[door]
}
}
// Print the doors that are open
for i, open in is_open {
if open {
println("Door #${i + 1} is open.")
}
}
}

View file

@ -0,0 +1,10 @@
@echo off
setlocal enableDelayedExpansion
:: 0 = closed
:: 1 = open
:: SET /A treats undefined variable as 0
:: Negation operator ! must be escaped because delayed expansion is enabled
for /l %%p in (1 1 100) do for /l %%d in (%%p %%p 100) do set /a "door%%d=^!door%%d"
for /l %%d in (1 1 100) do if !door%%d!==1 (
echo door %%d is open
) else echo door %%d is closed

View file

@ -0,0 +1,9 @@
@echo off
setlocal enableDelayedExpansion
set /a square=1, incr=3
for /l %%d in (1 1 100) do (
if %%d neq !square! (echo door %%d is closed) else (
echo door %%d is open
set /a square+=incr, incr+=2
)
)

View file

@ -0,0 +1,12 @@
/* 0 means door is closed, 1 means door is open */
for (i = 0; i < 100; i++) {
for (j = i; j < 100; j += (i + 1)) {
d[j] = 1 - d[j] /* Toggle door */
}
}
"Open doors:
"
for (i = 0; i < 100; i++) {
if (d[i] == 1) (i + 1)
}

View file

@ -0,0 +1,3 @@
>"d">:00p1-:>:::9%\9/9+g2%!\:9v
$.v_^#!$::$_^#`"c":+g00p+9/9\%<
::<_@#`$:\*:+55:+1p27g1g+9/9\%9

View file

@ -0,0 +1 @@
1+:::*.9`#@_

View file

@ -0,0 +1,4 @@
108p0>:18p;;>:9g!18g9p08g]
*`!0\|+relet|-1`*aap81::+]
;::+1<r]!g9;>$08g1+:08paa[
*`#@_^._aa

View file

@ -0,0 +1,8 @@
var doors = [false] * 100
for i in 0..100 {
iter var j = i; j < 100; j += i + 1 {
doors[j] = !doors[j]
}
var state = doors[i] ? 'open' : 'closed'
echo 'Door ${i + 1} is ${state}'
}

View file

@ -0,0 +1,3 @@
for i in 1..101 {
echo 'Door ${i} is ${i ** 0.5 % 1 > 0 ? "closed" : "open"}'
}

View file

@ -0,0 +1 @@
for i in 1..11 echo 'Door ${i**2} is open'

View file

@ -0,0 +1,10 @@
Graphics 640,480
i=1
While ((i*i)<=100)
a$=i*i
DrawText a$,10,20*i
Print i*i
i=i+1
Wend
Flip
WaitKey

View file

@ -0,0 +1,81 @@
DEFINE PROCEDURE ''DIVIDE'' [A,B]:
BLOCK 0: BEGIN
IF A < B, THEN:
QUIT BLOCK 0;
CELL(0) <= 1;
OUTPUT <= 1;
LOOP AT MOST A TIMES:
BLOCK 2: BEGIN
IF OUTPUT * B = A, THEN:
QUIT BLOCK 0;
OUTPUT <= OUTPUT + 1;
IF OUTPUT * B > A, THEN:
BLOCK 3: BEGIN
OUTPUT <= CELL(0);
QUIT BLOCK 0;
BLOCK 3: END;
CELL(0) <= OUTPUT;
BLOCK 2: END;
BLOCK 0: END.
DEFINE PROCEDURE ''MINUS'' [A,B]:
BLOCK 0: BEGIN
IF A < B, THEN:
QUIT BLOCK 0;
LOOP AT MOST A TIMES:
BLOCK 1: BEGIN
IF OUTPUT + B = A, THEN:
QUIT BLOCK 0;
OUTPUT <= OUTPUT + 1;
BLOCK 1: END;
BLOCK 0: END.
DEFINE PROCEDURE ''MODULUS'' [A,B]:
BLOCK 0: BEGIN
CELL(0) <= DIVIDE[A,B];
OUTPUT <= MINUS[A,CELL(0) * B];
BLOCK 0: END.
DEFINE PROCEDURE ''TOGGLE'' [DOOR]:
BLOCK 0: BEGIN
IF DOOR = 1, THEN:
QUIT BLOCK 0;
OUTPUT <= 1;
BLOCK 0: END.
DEFINE PROCEDURE ''NUMBERS'' [DOOR, COUNT]:
BLOCK 0: BEGIN
CELL(0) <= 1; /*each number*/
OUTPUT <= 0; /*current door state*/
LOOP COUNT TIMES:
BLOCK 1: BEGIN
IF MODULUS[DOOR, CELL(0)] = 0, THEN:
OUTPUT <= TOGGLE[OUTPUT];
CELL(0) <= CELL(0) + 1;
BLOCK 1: END;
BLOCK 0: END.
DEFINE PROCEDURE ''DOORS'' [COUNT]:
BLOCK 0: BEGIN
CELL(0) <= 1; /*each door*/
LOOP COUNT TIMES:
BLOCK 1: BEGIN
CELL(1) <= NUMBERS[CELL(0), COUNT]; /*iterate over the states of this door to get its final state*/
IF CELL(1) = 1, THEN: /*door state = open*/
PRINT[CELL(0), ' '];
CELL(0) <= CELL(0) + 1;
BLOCK 1: END;
BLOCK 0: END.
DOORS[100];

View file

@ -0,0 +1,26 @@
( 100doors-tbl
= door step
. tbl$(doors.101) { Create an array. Indexing is 0-based. Add one extra for addressing element nr. 100 }
& 0:?step
& whl
' ( 1+!step:~>100:?step { ~> means 'not greater than', i.e. 'less than or equal' }
& 0:?door
& whl
' ( !step+!door:~>100:?door
& 1+-1*!(!door$doors):?doors { <number>$<variable> sets the current index, which stays the same until explicitly changed. }
)
)
& 0:?door
& whl
' ( 1+!door:~>100:?door
& out
$ ( door
!door
is
( !(!door$doors):1&open
| closed
)
)
)
& tbl$(doors.0) { clean up the array }
)

View file

@ -0,0 +1,30 @@
( 100doors-var
= step door
. 0:?door
& whl
' ( 1+!door:~>100:?door
& closed:?!door { this creates a variable and assigns a value 'closed' to it }
)
& 0:?step
& whl
' ( 1+!step:~>100:?step
& 0:?door
& whl
' ( !step+!door:~>100:?door
& ( !!door:closed&open
| closed
)
: ?!door
)
)
& 0:?door
& whl
' ( 1+!door:~>100:?door
& out$(door !door is !!door)
)
& 0:?door
& whl
' ( 1+!door:~>100:?door
& tbl$(!door.0) { cleanup the variable }
)
)

View file

@ -0,0 +1,25 @@
( 100doors-list
= doors door doorIndex step
. :?doors
& 0:?door
& whl
' ( 1+!door:~>100:?door
& closed !doors:?doors
)
& 0:?skip
& whl
' ( :?ndoors
& whl
' ( !doors:?skipped [!skip %?door ?doors { the [<number> pattern only succeeds when the scanning cursor is at position <number> }
& !ndoors
!skipped
( !door:open&closed
| open
)
: ?ndoors
)
& !ndoors !doors:?doors
& 1+!skip:<100:?skip
)
& out$!doors
)

View file

@ -0,0 +1,22 @@
( 100doors-obj
= doors door doorIndex step
. :?doors
& 0:?door
& whl
' ( 1+!door:~>100:?door
& new$(=closed) !doors:?doors
)
& 0:?skip
& whl
' ( !doors:?tododoors
& whl
' ( !tododoors:? [!skip %?door ?tododoors
& ( !(door.):open&closed
| open
)
: ?(door.)
)
& 1+!skip:<100:?skip
)
& out$!doors
)

View file

@ -0,0 +1,4 @@
100doors-tbl$
& 100doors-var$
& 100doors-list$
& 100doors-obj$;

View file

@ -0,0 +1,2 @@
blsq ) 10ro2?^
{1 4 9 16 25 36 49 64 81 100}

View file

@ -0,0 +1,16 @@
#include <iostream>
int main()
{
bool is_open[100] = { false };
// do the 100 passes
for (int pass = 0; pass < 100; ++pass)
for (int door = pass; door < 100; door += pass+1)
is_open[door] = !is_open[door];
// output the result
for (int door = 0; door < 100; ++door)
std::cout << "door #" << door+1 << (is_open[door]? " is open." : " is closed.") << std::endl;
return 0;
}

View file

@ -0,0 +1,19 @@
#include <iostream>
int main()
{
int square = 1, increment = 3;
for (int door = 1; door <= 100; ++door)
{
std::cout << "door #" << door;
if (door == square)
{
std::cout << " is open." << std::endl;
square += increment;
increment += 2;
}
else
std::cout << " is closed." << std::endl;
}
return 0;
}

View file

@ -0,0 +1,7 @@
#include <iostream> //compiled with "Dev-C++" , from RaptorOne
int main()
{
for(int i=1; i*i<=100; i++)
std::cout<<"Door "<<i*i<<" is open!"<<std::endl;
}

View file

@ -0,0 +1,125 @@
#include <iostream> // compiled with clang (tags/RELEASE_600/final)
#include <type_traits> // or g++ (GCC) 7.3.1 20180406 -- from hare1039
namespace functional_list // basic building block for template meta programming
{
struct NIL
{
using head = NIL;
using tail = NIL;
friend std::ostream& operator << (std::ostream& os, NIL const) { return os; }
};
template <typename H, typename T = NIL>
struct list
{
using head = H;
using tail = T;
};
template <int i>
struct integer
{
static constexpr int value = i;
friend std::ostream& operator << (std::ostream& os, integer<i> const) { os << integer<i>::value; return os;}
};
template <typename L, int nTH> constexpr
auto at()
{
if constexpr (nTH == 0)
return (typename L::head){};
else if constexpr (not std::is_same_v<typename L::tail, NIL>)
return at<typename L::tail, nTH - 1>();
else
return NIL{};
}
template <typename L, int nTH>
using at_t = decltype(at<L, nTH>());
template <typename L, typename elem> constexpr
auto prepend() { return list<elem, L>{}; }
template <typename L, typename elem>
using prepend_t = decltype(prepend<L, elem>());
template <int Size, typename Dat = integer<0>> constexpr
auto gen_list()
{
if constexpr (Size == 0)
return NIL{};
else
{
using next = decltype(gen_list<Size - 1, Dat>());
return prepend<next, Dat>();
}
}
template <int Size, typename Dat = integer<0>>
using gen_list_t = decltype(gen_list<Size, Dat>());
} namespace fl = functional_list;
constexpr int door_amount = 101; // index from 1 to 100
template <typename L, int current, int moder> constexpr
auto construct_loop()
{
using val_t = fl::at_t<L, current>;
if constexpr (std::is_same_v<val_t, fl::NIL>)
return fl::NIL{};
else
{
constexpr int val = val_t::value;
using val_add_t = fl::integer<val + 1>;
using val_old_t = fl::integer<val>;
if constexpr (current == door_amount)
{
if constexpr(current % moder == 0)
return fl::list<val_add_t>{};
else
return fl::list<val_old_t>{};
}
else
{
using sub_list = decltype(construct_loop<L, current + 1, moder>());
if constexpr(current % moder == 0)
return fl::prepend<sub_list, val_add_t>();
else
return fl::prepend<sub_list, val_old_t>();
}
}
}
template <int iteration> constexpr
auto construct()
{
if constexpr (iteration == 1) // door index = 1
{
using l = fl::gen_list_t<door_amount>;
return construct_loop<l, 0, iteration>();
}
else
{
using prev_iter_list = decltype(construct<iteration - 1>());
return construct_loop<prev_iter_list, 0, iteration>();
}
}
template <typename L, int pos> constexpr
void show_ans()
{
if constexpr (std::is_same_v<typename L::head, fl::NIL>)
return;
else
{
if constexpr (L::head::value % 2 == 1)
std::cout << "Door " << pos << " is opened.\n";
show_ans<typename L::tail, pos + 1>();
}
}
int main()
{
using result = decltype(construct<100>());
show_ans<result, 0>();
}

View file

@ -0,0 +1,42 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main(string[] args)
{
bool[] doors = new bool[100];
//Close all doors to start.
for (int d = 0; d < 100; d++) doors[d] = false;
//For each pass...
for (int p = 0; p < 100; p++)//number of passes
{
//For each door to toggle...
for (int d = 0; d < 100; d++)//door number
{
if ((d + 1) % (p + 1) == 0)
{
doors[d] = !doors[d];
}
}
}
//Output the results.
Console.WriteLine("Passes Completed!!! Here are the results: \r\n");
for (int d = 0; d < 100; d++)
{
if (doors[d])
{
Console.WriteLine(String.Format("Door #{0}: Open", d + 1));
}
else
{
Console.WriteLine(String.Format("Door #{0}: Closed", d + 1));
}
}
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,21 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main(string[] args)
{
//Perform the operation.
bool[] doors = new bool[100];
int n = 0;
int d;
while ((d = (++n * n)) <= 100)
doors[d - 1] = true;
//Perform the presentation.
for (d = 0; d < doors.Length; d++)
Console.WriteLine("Door #{0}: {1}", d + 1, doors[d] ? "Open" : "Closed");
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,19 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main()
{
bool[] doors = new bool[100];
//The number of passes can be 1-based, but the number of doors must be 0-based.
for (int p = 1; p <= 100; p++)
for (int d = p - 1; d < 100; d += p)
doors[d] = !doors[d];
for (int d = 0; d < 100; d++)
Console.WriteLine("Door #{0}: {1}", d + 1, doors[d] ? "Open" : "Closed");
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,16 @@
namespace ConsoleApplication1
{
using System;
class Program
{
static void Main()
{
double n;
//If the current door number is the perfect square of an integer, say it is open, else say it is closed.
for (int d = 1; d <= 100; d++)
Console.WriteLine("Door #{0}: {1}", d, (n = Math.Sqrt(d)) == (int)n ? "Open" : "Closed");
Console.ReadKey(true);
}
}
}

View file

@ -0,0 +1,96 @@
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.Clear();
Console.WriteLine("Input a number of doors to calculate, then press enter");
StartCalculator();
}
static void StartCalculator()
{
//The number to calculate is input here
string input = Console.ReadLine();
Console.Clear();
try
{
//The program attempts to convert the string to an int
//Exceptions will be caught on this line
int numberOfDoors = Convert.ToInt32(input);
//Will call method recursively if input number is less than 1
if (numberOfDoors <= 0)
{
Console.WriteLine("Please use a number greater than 0");
StartCalculator();
}
//The program then starts the calculation process
Calculate(numberOfDoors);
//After calculation process is finished, restart method is called
RestartCalculator();
}
catch(FormatException)
{
//Code will be executed if the number has a decimal or has an unrecognizable symbol
Console.WriteLine("Unable to read. Please use a real number without a decimal");
StartCalculator();
}
catch (OverflowException)
{
//Code will be executed if number is too long
Console.WriteLine("You number is too long");
StartCalculator();
}
}
static void Calculate(int numberOfDoors)
{
//Increases numberOfDoors by 1 since array starts at 0
numberOfDoors++;
//Dictionary key represents door number, value represents if the door is open
//if value == true, the door is open
Dictionary<int, bool> doors = new Dictionary<int, bool>();
//Creates Dictionary size of numberOfDoors, all initialized at false
for(int i = 0; i < numberOfDoors; i++)
{
doors.Add(i, false);
}
//Creates interval between doors, starting at 0, while less than numberOfDoors
for (int doorInterval = 0; doorInterval < numberOfDoors; doorInterval++)
{
//Will alter every cubby at doorInterval
//1 needs to be added since doorInterval will start at 0 and end when equal to numberOfDoors
for(int i = 0; i < numberOfDoors; i += doorInterval + 1)
{
//Changes a false value to true and vice versa
doors[i] = doors[i] ? false: true;
}
}
//Writes each door and whether it is open or closed
for(int i = 0; i < numberOfDoors; i++)
{
//Skips over door 0
if (i == 0) continue;
//Writes open if door value is true, writes closed if door value is false
Console.WriteLine("Door " + (i) + " is " + (doors[i] ? "open" : "closed"));
}
}
static void RestartCalculator()
{
Console.WriteLine("Press any key to restart");
Console.ReadKey(true);
Main();
}
}

View file

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
char is_open[100] = { 0 };
int pass, door;
/* 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)
printf("door #%d is %s.\n", door+1, (is_open[door]? "open" : "closed"));
return 0;
}

View file

@ -0,0 +1,22 @@
#include <stdio.h>
#define NUM_DOORS 100
int main(int argc, char *argv[])
{
int is_open[NUM_DOORS] = { 0 } ;
int * doorptr, * doorlimit = is_open + NUM_DOORS ;
int pass ;
/* do the N passes, go backwards because the order is not important */
for ( pass= NUM_DOORS ; ( pass ) ; -- pass ) {
for ( doorptr= is_open + ( pass-1 ); ( doorptr < doorlimit ) ; doorptr += pass ) {
( * doorptr ) ^= 1 ;
}
}
/* output results */
for ( doorptr= is_open ; ( doorptr != doorlimit ) ; ++ doorptr ) {
printf("door #%lld is %s\n", ( doorptr - is_open ) + 1, ( * doorptr ) ? "open" : "closed" ) ;
}
}

View file

@ -0,0 +1,19 @@
#include <stdio.h>
int main()
{
int square = 1, increment = 3, door;
for (door = 1; door <= 100; ++door)
{
printf("door #%d", door);
if (door == square)
{
printf(" is open.\n");
square += increment;
increment += 2;
}
else
printf(" is closed.\n");
}
return 0;
}

View file

@ -0,0 +1,9 @@
#include <stdio.h>
int main()
{
int door, square, increment;
for (door = 1, square = 1, increment = 1; door <= 100; door++ == square && (square += increment += 2))
printf("door #%d is %s.\n", door, (door == square? "open" : "closed"));
return 0;
}

View file

@ -0,0 +1,10 @@
#include <stdio.h>
int main()
{
int i;
for (i = 1; i * i <= 100; i++)
printf("door %d open\n", i * i);
return 0;
}

Some files were not shown because too many files have changed in this diff Show more