User:Thebigh/mysandbox: Difference between revisions
(more) |
m (tweak) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Some FRACTRAN programs in case we ever have a category for it |
|||
An evil wizard has trapped you in the stairwell of his castle. Your only hope of escape is to run up all 100 steps to reach the top, and you can run one step per second. Unfortunately the wizard uses magic to lengthen the staircase by five steps per second. The new steps are inserted randomly between existing steps, so if you're lucky some of them might be beneath you and not ahead of you. |
|||
==A+B== |
|||
Can you escape, or are you doomed to run up an ever-lengthening staircase forever? |
|||
Input a number of the form 2^a 3^b |
|||
Write a program to simulate your escape attempt. How are you doing after ten minutes? For every second between 600 and 609 seconds inclusive print the number of steps behind you and the number still ahead of you. |
|||
<lang fractran> |
|||
2/3 |
|||
</lang> |
|||
The output is 2^(a+b) |
|||
==Empty program== |
|||
If you escaped, run 10,000 tests and print the average time taken and the average final length of the staircase. |
|||
A list of no fractions does nothing, then immediately stops. |
|||
==header|C== |
|||
<lang |
<lang fractran></lang> |
||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <time.h> |
|||
==Integer Sequence== |
|||
int main(void) { |
|||
Given the number 1 as input the following program will, as its (3n-2)th step, produce the number 2^n. |
|||
int trial, secs_tot=0, steps_tot=0; //keep track of time and steps over all the trials |
|||
<lang fractran> 2/3, 9/2, 2/1</lang> |
|||
int sbeh, slen, wiz, secs; //all the variables related to an individual trial |
|||
time_t t; |
|||
srand((unsigned) time(&t)); //random number seed |
|||
printf( "Seconds steps behind steps ahead\n" ); |
|||
for( trial=1;trial<=10000;trial++ ) { //10000 attempts for the runner |
|||
sbeh = 0; slen = 100; secs = 0; // initialise this trial |
|||
while(sbeh<slen) { //as long as the runner is still on the stairs |
|||
sbeh+=1; //runner climbs a step |
|||
for(wiz=1;wiz<=5;wiz++) { //evil wizard conjures five new steps |
|||
if(rand()%slen < sbeh) |
|||
sbeh+=1; //maybe a new step is behind us |
|||
slen+=1; //either way, the staircase is longer |
|||
} |
|||
secs+=1; //one second has passed |
|||
if(trial==1&&599<secs&&secs<610) |
|||
printf("%d %d %d\n", secs, sbeh, slen-sbeh ); |
|||
} |
|||
secs_tot+=secs; |
|||
steps_tot+=slen; |
|||
} |
|||
printf( "Average secs taken: %f\n", secs_tot/10000.0 ); |
|||
printf( "Average final length of staircase: %f\n", steps_tot/10000.0 ); |
|||
return 0; |
|||
}</lang> |
|||
{{out}}<pre> |
|||
Seconds steps behind steps ahead |
|||
600 2147 953 |
|||
601 2151 954 |
|||
602 2156 954 |
|||
603 2160 955 |
|||
604 2166 954 |
|||
605 2169 956 |
|||
606 2174 956 |
|||
607 2180 955 |
|||
608 2184 956 |
|||
609 2188 957 |
|||
Average secs taken: 2921.493200 |
|||
Average final length of staircase: 14707.466000 |
|||
</pre> |
|||
==Logical operations== |
|||
==header|Fermat== |
|||
It's not so hard to code up all sixteen possible two-input logic gates, so here they are. The input is 2^a 3^b where a,b are zero or one and the output is 5^1 for true and 5^0 for false. Gates that return true when all their inputs are false additionally require the flag 11 to be set as input (ie 2^a*3^b*11)- any FRACTRAN program with the number 1 as input either stops without doing anything or loops forever. |
|||
<lang fermat> |
|||
time_tot:=0; {we'll keep track of the total time and steps across all the trials} |
|||
steps_tot:=0; |
|||
!!('Seconds ','Steps behind ','Steps ahead'); |
|||
for trial = 1 to 10 do |
|||
s_behind:=0; {initialise staircase and runner for this trial} |
|||
stair_len:=100; |
|||
time:=0; |
|||
while s_behind < stair_len do |
|||
s_behind:=s_behind + 1; |
|||
for wiz = 1 to 5 do |
|||
stair_len:=stair_len + 1; {evil wizard creates a step} {(mwaahahaha)} |
|||
if (Rand|stair_len) < s_behind then |
|||
s_behind:=s_behind + 1; |
|||
fi |
|||
od; |
|||
time:=time+1; {another second has passed} |
|||
if trial = 1 and 599<time and time<610 then |
|||
!!(time, ' ', s_behind, ' ',stair_len-s_behind) |
|||
fi; |
|||
od; |
|||
time_tot:=time_tot+time; |
|||
steps_tot:=steps_tot+stair_len; |
|||
od; |
|||
<lang fractran> |
|||
!!(time_tot/10000); |
|||
5/6, 1/2, 1/3 AND gate |
|||
!!(steps_tot/10000);</lang> |
|||
5/6, 5/2, 5/3 OR gate |
|||
{{out}}<pre> |
|||
1/22, 5/11 NOT gate (uses 11 as a halt flag, result of 2^a*11 is 5^not(a)) |
|||
Seconds Steps behind Steps ahead |
|||
1/6, 5/2, 5/3 XOR gate |
|||
1/66, 5/22, 5/33, 5/11 NAND gate (needs 11 flag) |
|||
601 2129 976 |
|||
5/66, 1/22, 1/33, 5/11 NXOR gate (needs flag) |
|||
602 2132 978 |
|||
1/66, 1/22, 1/33, 5/11 NOR gate (needs flag) |
|||
603 2136 979 |
|||
604 2142 978 |
|||
605 2146 979 |
|||
606 2151 979 |
|||
607 2156 979 |
|||
608 2162 978 |
|||
609 2166 979 |
|||
3697083 / 1250 |
|||
3722083 / 250 |
|||
</pre> |
|||
so much for all the commonly encountered ones, but there's still another eight to go. Most are obscure and of limited utility. |
|||
==header|FreeBASIC== |
|||
1/2, 1/3 ZERO gate, returns false regardless of its input |
|||
<lang freebasic> |
|||
1/6, 5/2, 1/3 "A and not B", true only if A is true and B is false |
|||
randomize timer |
|||
5/2, 1/3 A , returns the state of A regardless of B |
|||
dim as uinteger steps_behind = 0, stairs_length = 100, seconds, j |
|||
1/6, 1/2, 5/3 "B and not A", true only if B is true and A is false |
|||
dim as uinteger seconds_tot, steps_tot |
|||
1/2, 5/3 B , returns the state of B regardless of A |
|||
print "Seconds", "steps behind", "steps ahead" |
|||
1/66, 1/33, 5/11 "A or not B" (needs flag) |
|||
for trial as uinteger = 1 to 10000 'We'll have the runner try this 10000 times |
|||
1/66, 1/22, 5/11 "B or not A" (needs flag) |
|||
steps_behind = 0 'runner starts at the bottom |
|||
5/66, 5/22, 5/33, 5/11 ONE gate, returns true regardless of its input, needs flag |
|||
seconds = 0 'reset time taken |
|||
stairs_length = 100 'Staircase has 100 steps |
|||
while steps_behind < stairs_length 'if the runner hasn't reached the top |
|||
steps_behind += 1 'go up one step |
|||
for j = 1 to 5 'The evil wizard conjures another five steps |
|||
if int(rnd*stairs_length) < steps_behind then steps_behind += 1 |
|||
'there's a chance that a new step will be behind you |
|||
stairs_length += 1 'but either way the staircase is one step longer |
|||
next j |
|||
seconds += 1 'that all took one second |
|||
if trial = 1 and seconds >599 and seconds < 610 then print seconds, steps_behind, stairs_length - steps_behind |
|||
'for the first attempt, see how the runner is doing after ten minutes |
|||
wend |
|||
seconds_tot += seconds 'if the runner escaped, track the time taken and the length of the stairs |
|||
steps_tot += stairs_length |
|||
next trial |
|||
NOT A and NOT B are omitted because the one-input NOT gate is already up there. |
|||
print "Average time taken: ";seconds_tot/10000; " seconds." |
|||
</lang> |
|||
print "Average final staircase length: ";steps_tot/10000; " steps." |
|||
'if you noticed that last number is about 100*exp(5), that's no coincidence</lang> |
|||
==Sort three variables== |
|||
{{out}}<pre> |
|||
FRACTRAN's only data type is positive integers. Suppose (a,b,c) are the integers to be sorted. Give the following as input: |
|||
Seconds steps behind steps ahead |
|||
2^a 3^b 5^c |
|||
600 2032 1068 |
|||
<lang fractran> |
|||
601 2038 1067 |
|||
1001/30, 143/6, 143/10, 143/15, 13/2, 13/3, 13/5 |
|||
602 2042 1068 |
|||
</lang> |
|||
603 2045 1070 |
|||
Returns 7^A 11^B 13^C where (A,B,C) are (a,b,c) in ascending order. |
|||
604 2048 1072 |
|||
605 2053 1072 |
|||
606 2055 1075 |
|||
607 2060 1075 |
|||
608 2064 1076 |
|||
609 2068 1077 |
|||
Average time taken: 2921.9457 seconds. |
|||
Average final staircase length: 14709.7285 steps. |
|||
</pre> |
Latest revision as of 11:27, 28 November 2021
Some FRACTRAN programs in case we ever have a category for it
A+B
Input a number of the form 2^a 3^b <lang fractran> 2/3 </lang> The output is 2^(a+b)
Empty program
A list of no fractions does nothing, then immediately stops. <lang fractran></lang>
Integer Sequence
Given the number 1 as input the following program will, as its (3n-2)th step, produce the number 2^n. <lang fractran> 2/3, 9/2, 2/1</lang>
Logical operations
It's not so hard to code up all sixteen possible two-input logic gates, so here they are. The input is 2^a 3^b where a,b are zero or one and the output is 5^1 for true and 5^0 for false. Gates that return true when all their inputs are false additionally require the flag 11 to be set as input (ie 2^a*3^b*11)- any FRACTRAN program with the number 1 as input either stops without doing anything or loops forever.
<lang fractran> 5/6, 1/2, 1/3 AND gate 5/6, 5/2, 5/3 OR gate 1/22, 5/11 NOT gate (uses 11 as a halt flag, result of 2^a*11 is 5^not(a)) 1/6, 5/2, 5/3 XOR gate 1/66, 5/22, 5/33, 5/11 NAND gate (needs 11 flag) 5/66, 1/22, 1/33, 5/11 NXOR gate (needs flag) 1/66, 1/22, 1/33, 5/11 NOR gate (needs flag)
so much for all the commonly encountered ones, but there's still another eight to go. Most are obscure and of limited utility.
1/2, 1/3 ZERO gate, returns false regardless of its input 1/6, 5/2, 1/3 "A and not B", true only if A is true and B is false 5/2, 1/3 A , returns the state of A regardless of B 1/6, 1/2, 5/3 "B and not A", true only if B is true and A is false 1/2, 5/3 B , returns the state of B regardless of A 1/66, 1/33, 5/11 "A or not B" (needs flag) 1/66, 1/22, 5/11 "B or not A" (needs flag) 5/66, 5/22, 5/33, 5/11 ONE gate, returns true regardless of its input, needs flag
NOT A and NOT B are omitted because the one-input NOT gate is already up there. </lang>
Sort three variables
FRACTRAN's only data type is positive integers. Suppose (a,b,c) are the integers to be sorted. Give the following as input: 2^a 3^b 5^c <lang fractran> 1001/30, 143/6, 143/10, 143/15, 13/2, 13/3, 13/5 </lang> Returns 7^A 11^B 13^C where (A,B,C) are (a,b,c) in ascending order.