Even in C, we can do this without any conversions (i.e. convert a number to string) we can directly read a string and do this

#include <stdio.h> #include <string.h> int main(int argc, char **argv) { char *str = malloc(strlen(argv[1]); strcpy(argv[1], str); char last = str[strlen(str) - 1]; switch (last){ case '0': case '2': case '4': case '6': case '8': case 'A': case 'a': case 'C': case 'c': case 'E': case 'e': printf("even\n"); break; default: printf("odd\n"); break; }

A caveat, in search of a better word, would be the string allocation and copying routine. I used the standard string library functions but it will still take time even though the actual logic (the `switch` statement) is really fast.

As for the generalization of the idea, yes, this logic is applicable only for mod2 and mod5 machines where only 2 states are sufficient. And yeah, this is applicable for all bases that are of the form 2^n and base 10 (example, convert 112358 base 10 to base 32 and 11235813 base 10 to base 32).

For bases >16, we need to go beyond `F' for example, in base 32, 15 base 10 will be represented as F and 16 as G. Therefore, in base 32, if the number ends with a G, it is even.

*In reply to* **Re^2: A mod2 Machine.**
*by* **code-ninja**
*in thread* **A mod2 Machine.**
*by* **code-ninja**

- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr

For: |
Use: |
||

& | & | ||

< | < | ||

> | > | ||

[ | [ | ||

] | ] |