Created
April 5, 2025 10:39
-
-
Save zr-tex8r/a0863374fd049dee2eff5ace8cfb4344 to your computer and use it in GitHub Desktop.
TeX:TeX言語で整数が奇数であるかを判定する(ローマ数字編)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 準備 | |
\newif\ifIsOdd | |
\newcount\myCount | |
\newbox\myBox | |
%% \CheckOdd<整数n>: nが奇数であるかを判定し, 結果を | |
% スイッチ \ifIsOdd に返す. | |
\def\CheckOdd#1{% | |
\IsOddfalse | |
% nが奇数 ⇔ 5nのローマ数字の末尾が'v' | |
% であるため, ボックス中でローマ数字を出力した上で | |
% \spacefactor を利用して末尾が'v'であるかを判定する. | |
\setbox\myBox\hbox{% | |
% 'v'のspacefactorを変更する(他の英小文字は1000) | |
\sfcode`v=1001 | |
% 5nのローマ数字を出力する | |
\myCount=#1\relax | |
\multiply\myCount5 | |
\romannumeral\myCount\relax | |
% 現在のspacefactorが1001なら末尾は'v'である | |
\ifnum\spacefactor>1000 | |
\aftergroup\IsOddtrue | |
\fi | |
}% | |
} | |
%----------------------------------------------------------- | |
% 1から100までの整数についてテストする. | |
\myCount=0 \loop\ifnum\myCount<100 | |
\advance\myCount1 | |
\the\myCount\space is | |
\CheckOdd\myCount | |
\ifIsOdd odd\else even\fi.\endgraf | |
\repeat | |
\bye |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment