Skip to content

Instantly share code, notes, and snippets.

@wtsnjp
Created December 21, 2022 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtsnjp/382c48cac433160476f3311cb4b9eeff to your computer and use it in GitHub Desktop.
Save wtsnjp/382c48cac433160476f3311cb4b9eeff to your computer and use it in GitHub Desktop.
NabeAzz in WEB
% nabeazz.web
\centerline{\bf A NabeAzz Program}
\centerline{\sl by wtsnjp}
\vskip .5cm
This is my first program to write with the \.{WEB} system. This program is a
solution of the NabeAzz problem, which is a well-known problem in Japanese
{\TeX} community as the counterpart to the FizzBuzz problem
(see \.{https://zrbabbler.hatenablog.com/entry/20110815/1313398638}).
@ First of all, decide how many numbers to print.
@d boundary=400 { This will be the maximum number to be printed }
@ This is the top level definition.
@p
program NabeAzz(output);
@<some routines@>
var
n: integer;
begin
writeln( @<prologue@> );
@<the main loop@>
writeln( @<epilogue@> );
end.
@ Here are the very simple prologue: we just setup the {\it aho} (silly) font.
@<prologue@>=
'\font\cmfi=cmfi10 at 12pt \noindent'
@ The number in {\it aho} form should be output with the font defined above.
@<procedure to write number in aho form@>=
procedure WriteAhoNumber(n: integer);
begin
write('{\cmfi ');
write(n);
writeln('}')
end;
@ The epilogue is also quite simple.
@<epilogue@>=
'\bye'
@ The main part of the program. Loop the number from one to the boundary.
Each number should be printed in either normal or {\it aho} form.
@<the main loop@>=
for n := 1 to boundary do AhoOrNot(n);
@ Now, let us think about the actual logic for the NabeAzz program.
@<some routines@>=
@<procedure to write number in aho form@>
@<function to judge if a multiple of three@>
@<function to judge if a number that has a digit of three@>
procedure AhoOrNot(n: integer);
begin
if MultipleOfThree(n) then
WriteAhoNumber(n)
else if HasDigitOfThree(n) then
WriteAhoNumber(n)
else writeln(n)
end;
@ The first condition to be {\it aho} is being a multiple of three.
This can be easily judged by using the mod operator.
@<function to judge if a multiple of three@>=
function MultipleOfThree(n: integer) : boolean;
begin
if n mod 3 <> 0 then
MultipleOfThree := false
else
MultipleOfThree := true
end;
@ The second condition to be {\it aho} is having the digit three.
This also can be easily judged by using some basic string operations.
@<function to judge if a number that has a digit of three@>=
function HasDigitOfThree(n: integer) : boolean;
var
s: string;
begin
str(n, s);
if pos('3', s) <> 0 then
HasDigitOfThree := true
else
HasDigitOfThree := false
end;
@wtsnjp
Copy link
Author

wtsnjp commented Dec 21, 2022

Makefile

# public domain
PROGRAM  = nabeazz
TANGLE 	 = tangle
WEAVE    = weave
COMPILER = fpc
PDFTEX   = pdftex

.PHONY: all
all: output.pdf

$(PROGRAM): $(PROGRAM).web
	$(TANGLE) $(PROGRAM).web
	$(COMPILER) $(PROGRAM).p

output.pdf: $(PROGRAM)
	./$(PROGRAM) > output.tex
	$(PDFTEX) output.tex

.PHONY: doc
doc:
	make $(PROGRAM).pdf

$(PROGRAM).pdf: $(PROGRAM).web
	$(WEAVE) $(PROGRAM).web
	$(PDFTEX) $(PROGRAM).tex

.PHONY: clean
clean:
	rm -f $(PROGRAM) $(PROGRAM).{p,o}
	rm -f CONTENTS.tex $(PROGRAM).{tex,log,pdf}
	rm -f output.{tex,pdf,log}

Usage

The WEB system (tangle and weave) and a Pascal compiler (such as fpc) are required.

To produce the NabeAzz output to output.pdf:

make

To produce the documentation in nabeazz.pdf:

make doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment