pad.mattdiesel.co.uk

Snippet - FizzBuzz

FizzBuzz (C)

A completely unrolled fizzbuzz
Created 2015-08-11 21:58:48.114940 by Matt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>

void fizzbuzz(int upto)
{
	const char* fizz = "fizz", *fizzbuzz = "fizzbuzz", *buzz = &fizzbuzz[4];
	char s[16] = { '\0' }, *es = &s[14], *eb = &s[14], *et;

	es[0] = '1';

	for (;upto; upto -= 15)
	{
		puts(eb);
		es[0]++;
		puts(eb);
		puts(fizz);
		es[0] += 2;
		puts(eb);
		puts(buzz);
		puts(fizz);
		if (es[0] > '6')
		{
			es[0] -= 7;
			et = es-1;
			while (et[0]++ == '9')
				(et--)[0] = '0';
			if (et < eb)
			{
				et[0] = '1';
				eb = et;
			}
		}
		else
			es[0] += 3;
		puts(eb);
		es[0]++;
		puts(eb);
		puts(fizz);
		puts(buzz);
		if (es[0] > '6')
		{
			es[0] -= 7;
			et = es-1;
			while (et[0]++ == '9')
				(et--)[0] = '0';
			if (et < eb)
			{
				et[0] = '1';
				eb = et;
			}
		}
		else
			es[0] += 3;
		puts(eb);
		puts(fizz);
		es[0] += 2;
		puts(eb);
		es[0]++;
		puts(eb);
		puts(fizzbuzz);
		if (es[0] > '7')
		{
			es[0] -= 8;
			et = es-1;
			while (et[0]++ == '9')
				(et--)[0] = '0';
			if (et < eb)
			{
				et[0] = '1';
				eb = et;
			}
		}
		else
			es[0] += 2;
	}
}

int main()
{
	fizzbuzz(9000);

	return 0;
}