Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
56
Task/Fractran/Batch-File/fractran.bat
Normal file
56
Task/Fractran/Batch-File/fractran.bat
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
::Set the inputs
|
||||
set "code=17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1"
|
||||
set "n=2"
|
||||
|
||||
::Basic validation of code
|
||||
for %%. in (!code!) do (
|
||||
echo.%%.|findstr /r /c:"^[0-9][0-9]*/[1-9][0-9]*$">nul||goto error_code
|
||||
)
|
||||
::Validate the input
|
||||
set /a "tst=1*!n!" 2>nul
|
||||
if !tst! lss 0 goto error_input
|
||||
if !tst! equ 0 (if not "!n!"=="0" (goto error_input))
|
||||
|
||||
::Set the limit outputs
|
||||
set limit=20
|
||||
|
||||
::Execute the code
|
||||
echo.Input:
|
||||
echo. !n!
|
||||
echo.Output:
|
||||
for /l %%? in (1,1,!limit!) do (
|
||||
set shouldwehalt=1
|
||||
for %%A in (!code!) do (
|
||||
for /f "tokens=1,2 delims=/" %%B in ("%%A") do (
|
||||
set /a "tst=!n! %% %%C"
|
||||
if !tst! equ 0 (
|
||||
if !shouldwehalt! equ 1 (
|
||||
set shouldwehalt=0
|
||||
set /a "n=n*%%B/%%C"
|
||||
echo. !n!
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
if !shouldwehalt! equ 1 goto halt
|
||||
)
|
||||
|
||||
:halt
|
||||
echo.
|
||||
pause
|
||||
exit /b 0
|
||||
|
||||
:error_code
|
||||
echo.Syntax error in code.
|
||||
echo.
|
||||
pause
|
||||
exit /b 1
|
||||
|
||||
:error_input
|
||||
echo.Invalid input.
|
||||
echo.
|
||||
pause
|
||||
exit /b 1
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
#include <sstream>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
@ -33,13 +33,13 @@ public:
|
|||
private:
|
||||
void exec( vector< pair<float, float> >* v )
|
||||
{
|
||||
int cnt = 0; bool found; float r;
|
||||
int cnt = 0;
|
||||
while( cnt < limit )
|
||||
{
|
||||
cout << cnt << " : " << start << "\n";
|
||||
cnt++;
|
||||
vector< pair<float, float> >::iterator it = v->begin();
|
||||
found = false;
|
||||
bool found = false; float r;
|
||||
while( it != v->end() )
|
||||
{
|
||||
r = start * ( ( *it ).first / ( *it ).second );
|
||||
|
|
@ -60,5 +60,6 @@ private:
|
|||
int main( int argc, char* argv[] )
|
||||
{
|
||||
fractran f; f.run( "17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1", 2, 15 );
|
||||
return system( "pause" );
|
||||
cin.get();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
sub ft (\n) {
|
||||
first Int, map (* * n).narrow,
|
||||
<17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1>, 0
|
||||
|<17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1>, 0
|
||||
}
|
||||
constant FT = 2, &ft ... 0;
|
||||
say FT[^100];
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
constant FT = 2, &ft ... 0;
|
||||
constant FT2 = FT.grep: { not $_ +& ($_ - 1) }
|
||||
for 1..* -> $i {
|
||||
given FT2[$i] {
|
||||
|
|
|
|||
24
Task/Fractran/Scala/fractran.scala
Normal file
24
Task/Fractran/Scala/fractran.scala
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class TestFractran extends FunSuite {
|
||||
val program = Fractran("17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1")
|
||||
val expect = List(2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, 910, 170, 156, 132)
|
||||
|
||||
test("find first fifteen fractran figures") {
|
||||
assert((program .execute(2) take 15 toList) === expect)
|
||||
}
|
||||
}
|
||||
|
||||
object Fractran {
|
||||
val pattern = """\s*(\d+)\s*/\s*(\d+)\s*""".r
|
||||
def parse(m: Match) = ((m group 1).toInt, (m group 2).toInt)
|
||||
def apply(program: String) = new Fractran(
|
||||
pattern.findAllMatchIn(program).map(parse).toList)
|
||||
}
|
||||
|
||||
class Fractran(val numDem: List[(Int,Int)]) {
|
||||
def execute(value: Int) = unfold(value) { v =>
|
||||
numDem indexWhere(v % _._2 == 0) match {
|
||||
case i if i > -1 => Some(v, numDem(i)._1 * v / numDem(i)._2)
|
||||
case _ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue