in reply to Re: A mod2 Machine.

in thread A mod2 Machine.

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.

Replies are listed 'Best First'. | |
---|---|

Re^3: A mod2 Machine.
by hdb (Prior) on Jul 04, 2013 at 07:26 UTC |

In Section
Meditations

Comment onRe^2: A mod2 Machine.SelectorDownloadCode