Puzzle - RPL and others
|
05-11-2021, 10:37 PM
Post: #41
|
|||
|
|||
RE: Puzzle - RPL and others
(05-11-2021 02:14 PM)3298 Wrote:(05-11-2021 11:58 AM)Albert Chan Wrote: Without typecast, it may already be broken with left shift 31That would be sign-extension. I typedef'd bitset_t to be unsigned long long, which should as an unsigned type receive zero-extension instead. Albert is correct. Converting a narrower signed integer value to a wider unsigned type does result in sign extension, although the result will always be positive. For example, assigning a 32-bit signed integer value -1 to a 64-bit unsigned integer will produce the value 0xffffffffffffffff. Quote:C is certainly happy to stab you in the back when you treat it wrong. That is true! — Ian Abbott |
|||
05-13-2021, 11:38 PM
(This post was last modified: 05-14-2021 09:52 PM by Albert Chan.)
Post: #42
|
|||
|
|||
RE: Puzzle - RPL and others
(05-10-2021 03:57 AM)Albert Chan Wrote: Per your advise, I did just that, implemented gcd(2n,d) buckets. I translated Lua version to C, and added more optimization. Now it cache intermediate calculations, groups of 6 digits at a time. Timing is close to my unsigned fastmod version, n=60 timing at 13.3 sec BTW, Lua version comment about n ≤ ⌊ 2^(53/7) ⌋ = 190 were off. With double, it should be n ≤ ⌊ 2^(54/7) ⌋ = 210, since even digits are always even. I did not bother adding code to interpret the output, since solutions are so rare. This is expected output: c:\> polydivisible 10 { 3 38 381 3816 38165 381654 7 72 9 } c:\> polydivisible 14 { 9 138 1935 27100 379405 5311674 7 104 1467 20546 287645 4027032 13 } To convert back to base-14 digits, just recover 1 digit at a time: These digits does not need correction: (1st, 7th, 13th ...) + last 2nd digit = 138 - 9*14 = 12 3rd digit = 1935 - 138*14 = 3 ... I also use unsigned as little as possible (only gcd(a,b)) Danger – unsigned types used here! Code: #include <math.h> Update: fixed a bug Code: D = D * x[d-i] + x[d]; // D*n^p + last_digit It was possible for agrument inside fmod to reach size n^8. Added a guard against it, lowered it back down to n^7 Had we remove the guard, code still work, but base ≤ ⌊2^(55/8)⌋ = 117. Above code snippet had a flaw, but turns out harmless. if k = 1, then D = 0 * x[-6] + x[0] = 0 * n^0 + n^6 = n^6 This is just gibberish, but fmod() "fixed" it, since with integer x, fmod(x,1) = 0. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)