Re: [math-fun] Reciprocals of integers in binary
Since there was so much interest in reciprocals in factorial base, and since 19 wasn't very many, I decided to calculate *all* fractions between 0 and 1 exclusive whose numerators and denominators did not exceed 20. (Who am I kidding? I'm just using this as an excuse to procrastinate on debugging my negabinary code.) Note the countdown pattern in the last digit, and the countup pattern in the next to last digit, whenever the denominator is a prime. 1/2: 0.1 1/3: 0.02 2/3: 0.11 1/4: 0.012 3/4: 0.112 1/5: 0.0104 2/5: 0.0213 3/5: 0.1022 4/5: 0.1131 1/6: 0.01 5/6: 0.12 1/7: 0.003206 2/7: 0.012415 3/7: 0.022124 4/7: 0.101333 5/7: 0.111042 6/7: 0.120251 1/8: 0.003 3/8: 0.021 5/8: 0.103 7/8: 0.121 1/9: 0.00232 2/9: 0.01114 4/9: 0.02232 5/9: 0.10114 7/9: 0.11232 8/9: 0.12114 1/10: 0.0022 3/10: 0.0131 7/10: 0.1104 9/10: 0.1213 1/11: 0.002053140A 2/11: 0.0101462819 3/11: 0.0122424328 4/11: 0.0203355737 5/11: 0.0224317246 6/11: 0.1010250655 7/11: 0.1031212164 8/11: 0.1112143573 9/11: 0.1133105082 10/11: 0.1214036491 1/12: 0.002 5/12: 0.022 7/12: 0.102 11/12: 0.122 1/13: 0.00141254850C 2/13: 0.003325306A1B 3/13: 0.01124105542A 4/13: 0.013153613939 5/13: 0.021106362348 6/13: 0.023022120857 7/13: 0.100434669266 8/13: 0.102350427775 9/13: 0.110303176184 10/13: 0.112215734693 11/13: 0.1201314830A2 12/13: 0.1220442415B1 1/14: 0.001333 3/14: 0.011042 5/14: 0.020251 9/14: 0.103206 11/14: 0.112415 13/14: 0.122124 1/15: 0.0013 2/15: 0.0031 4/15: 0.0122 7/15: 0.0231 8/15: 0.1004 11/15: 0.1113 13/15: 0.1204 14/15: 0.1222 1/16: 0.00123 3/16: 0.01023 5/16: 0.01323 7/16: 0.02223 9/16: 0.10123 11/16: 0.11023 13/16: 0.11323 15/16: 0.12223 1/17: 0.001202368909270G 2/17: 0.0024047477154E1F 3/17: 0.010110326521762E 4/17: 0.01131270532A9D3D 5/17: 0.013015274136C54C 6/17: 0.020220652A430C5B 7/17: 0.02142323184C346A 8/17: 0.0231256106585B79 9/17: 0.1003311794648388 10/17: 0.102033558270AA97 11/17: 0.103236137079D2A6 12/17: 0.11044151598619B5 13/17: 0.11214408479241C4 14/17: 0.11334646359B68D3 15/17: 0.1210520423A790E2 16/17: 0.1222544211B3B7F1 1/18: 0.00114 5/18: 0.01232 7/18: 0.02114 11/18: 0.10232 13/18: 0.11114 17/18: 0.12232 1/19: 0.001116209526BBD80I 2/19: 0.002235418A5098AG1H 3/19: 0.00335462847775872G 4/19: 0.0110140379A1525F3F 5/19: 0.0121332474082E364E 6/19: 0.0132524569320B0E5D 7/19: 0.020411666358C7E56C 8/19: 0.022031075882A4BD7B 9/19: 0.0231502852A981948A 10/19: 0.1003065048135D6C99 11/19: 0.10142571423A3A43A8 12/19: 0.103045123764171BB7 13/19: 0.11020433318AD3F2C6 14/19: 0.1113235426B4B0CAD5 15/19: 0.11244275211B8CA1E4 16/19: 0.1201021616456979F3 17/19: 0.12122137106C4650G2 18/19: 0.1223405805962328H1 1/20: 0.0011 3/20: 0.0033 7/20: 0.0202 9/20: 0.0224 11/20: 0.1011 13/20: 0.1033 17/20: 0.1202 19/20: 0.1224 Here's my code. It's all mine, except for the small part I borrowed from Euclid to avoid duplicate fractions. #include <stdio.h> int main() { long long i,i2,j,k,n,n2,a[21],f[21]; char c; f[0] = 1; for (i=1;i<21;i++) f[i] = f[i-1]*i; for (i=2;i<21;i++) { for (n=1;n<i;n++) { i2 = i; n2 = n; while (i2 != n2) { if (i2 > n2) i2 -= n2; if (n2 > i2) n2 -= i2; } if (i2 == 1) { for (j=0;j<21;j++) a[j] = 0; a[i] = n*f[i-1]; for (j=i-1;j;j--) { a[j] = a[j+1]/(j+1); a[j+1] %= (j+1); } for (j=0;j<21;j++) if (a[j]) k=j; printf("%lld/%lld: 0.",n,i); for (j=2;j<k+1;j++) { c = a[j]+48; if (c>57) c=a[j]+55; printf("%c",c); } printf("\n"); } } printf("\n"); } return(0); }
participants (1)
-
Keith F. Lynch