Ask HN: x = 53; 4.times{ print (x = ( 139\*x - 12169 ) % 444).chr }
Iterating program for generating String.
Do you remember the math formulas for generating strings? I remember 'Linux' formula but not exactly.
example in ruby:
x = 53; 4.times{ print (x = ( 139*x - 12169 ) % 444).chr }
Could someone put other examples?
That’s just a simple linear congruential generator <https://en.wikipedia.org/wiki/Linear_congruential_generator>. In principle it’s possible to devise one that prints anything you want.
ok, I want word 'Hacker' please find me parameter ;-)
word 'Linux' will use real number not integer but I'm not remember what to do this. some variable must be 'floor' cut / rouded. I canot find any real solution
What if the word contains repeated letters?
Then it depends on where the repeated letters are, and still may not work. It might work if the repeat letters are the last letters, as in "all", or if it's only a sequence of repeat letters, as in "eeeeeeeee" which has a trivial solution (x0='e', a = 1, c = 0, m > 'e'). The reason is that once you start repeating you've hit a fixed point and you'll never escape it. So words like "hello" and "llama" cannot be generated with this method.
LCGs also won't create all arbitrary sequences (even if you dismiss the kind mentioned above and just want to generate the rest). I used Z3 to try and find parameters for "Linux" and it failed, while reliably producing an equivalent result for "Ruby" (a, c, and x0 were the same as the original modulo m, but not the exact same numbers) and solutions for other words. I could get as far as "Lin" but "Linu" seemed to be the breaking point.
Note that a solution might be possible if you change how you interpret the variable `x`. The original interprets `x` directly as a character value for printing which is a pretty severe restriction on the generated sequence. If you only require it to be correct modulo 128 or 256 (for 7- or 8-bit ASCII) then a solution might show up. In fact, that modulo (used for displaying, call it `d`) could also be a parameter to the formula for Z3 to try and find.
Here's "Ruby" from a Z3 run using this idea:
The printing function is: The "Linux" search was taking longer than I cared to let it run on my laptop, I don't know if there's a solution using this technique for it.Here's my z3 (Python) code, someone better versed in z3 ought to be able to improve this:
The lower bounds for `m` and `d` are necessary if you're going to generate every letter in the sequence so adding it at the start seemed reasonable. I restricted (not originally) a, b, and x0 to be within the bounds of [0,m) and (0,m) for x0 (0 is always fixed-point and won't be what we want to print for this exercise so it can be skipped). This doesn't seem to have a clear performance impact though I expected it to, it does make the printout clearer since everything is in a "reduced" form.Cool!
> If you only require it to be correct modulo 128 or 256 (for 7- or 8-bit ASCII) then a solution might show up.
Another variant I thought of is to not require the character to be picked from the least bits of x. Still seem very difficult to find the right parameters for generating arbitrary strings though.
m = 1062 a = 23 c = 984 x0 = 608