~rabbits/fractran

5821f895c302940cd30001d8ee6467c8b9c326ff — neauoire 10 months ago 453797a
*
3 files changed, 23 insertions(+), 19 deletions(-)

M README.md
M fractran
M fractran.c
M README.md => README.md +15 -12
@@ 17,23 17,26 @@ A valid input begins with a state, followed by a series of positive fractions. I
Adding the values store in the registers 2 and 3, and storing the result in the register 3.

```
144 3/2
3888 2/3
```

### Stepping

```
r02=04 r03=02
144 × 3/2 = 216/1 
r02=03 r03=03
216 × 3/2 = 324/1 
r02=02 r03=04
324 × 3/2 = 486/1 
r02=01 r03=05
486 × 3/2 = 729/1 
r03=06
729 × 3/2 = 2187/2 
r03=06
3888 2/3
r02=04 r03=05
3888 × 2/3 = 2592/1 
r02=05 r03=04
2592 × 2/3 = 1728/1 
r02=06 r03=03
1728 × 2/3 = 1152/1 
r02=07 r03=02
1152 × 2/3 = 768/1 
r02=08 r03=01
768 × 2/3 = 512/1 
r02=09
512 × 2/3 = 1024/3 
r02=09
```

## Multiplication Program

M fractran => fractran +0 -0
M fractran.c => fractran.c +8 -7
@@ 29,10 29,10 @@ gcd(int a, int b)
}

Fraction
Frac(int num, int den)
Frac(unsigned int num, unsigned int den)
{
	Fraction f;
	int d = gcd(num, den);
	unsigned int d = gcd(num, den);
	f.num = num / d;
	f.den = den / d;
	return f;


@@ 41,17 41,18 @@ Frac(int num, int den)
void
printstate(Machine *m)
{
	int fac = 2, num = m->state.num;
	unsigned int fac = 2, num = m->state.num;
	printf("[%d] ", num);
	while(num > 1) {
		if(num % fac == 0) {
			int pow = 1;
			printf("r%02d=", fac);
			unsigned int pow = 1;
			printf("r%02u=", fac);
			num /= fac;
			while(!(num % fac)) {
				num /= fac;
				pow++;
			}
			printf("%02d", pow);
			printf("%02u", pow);
			if(num != 1)
				putchar(' ');
		} else


@@ 67,7 68,7 @@ run(Machine *m)
	while(i < m->len) {
		Fraction res, *f = &m->program[i++];
		res = Frac(m->state.num * f->num, m->state.den * f->den);
		printf("%d × %d/%d = %d/%d \n",
		printf("%u × %u/%u = %u/%u \n",
			m->state.num,
			f->num,
			f->den,