One-million in binary is equal to 11110100001001000000
(20 bits). Thus, we cannot implement a simple counter for this.
Instead, the max number we can represent with 16 bits is 1111111111111111
= 65,535. Since the answer requires that we only need to approximate 1 million function runs, we could add one to the counter approximately once every 16 function runs, and know that the function has run about 1 million times when the counter reaches 1000000/16 = 62500.
Below is the pseudocode of a potential solution, assuming that counter
is a positive integer stored in 16 bits. Also, this assumes that calls such as random(1,16)
do not count towards memory as long as they are not stored in a variable. Last, the random(x,y)
function should give a random integer between x
and y
inclusive.
counter = 0;
function amIAttaBoutaMillionYet(){
if(random(1,16)==16 && counter<62501){
counter++;
}
if(counter==62500){
return true;
}
return false;
}
UPDATE
Fixed function so that it won't show true after about 2 million+, 3 million+, etc. function runs. (thanks trinithis!)
1What is the 16 bits? 16 bits of function definition in machine language? 16 bits of memory storage, but the function can be as large as necessary? What is this function supposed to do? Stop when it has been run one million times? Display a message when it has been run one million times? Is this function being randomly called by another program and you have to track, or is this function being run one million times in a row at the same time? Can you describe in pseudo-code how this function works/is used in this process? – mellamokb – 2011-08-24T17:35:16.133
2Superbly underspecified. – Thomas Eding – 2011-08-24T19:44:38.007
1I understand: store the number of invocations (or whatever you like) in 16 bits. But should be clarified. – user unknown – 2011-08-25T04:53:50.513
1Bearing in mind that a standard PRNG, which all the approachs so far are using, probably has at least 48 bits of internal state, that approach fails point 3. – Peter Taylor – 2011-08-25T06:10:55.580
@Peter Mine doesn't use random numbers... – Gareth – 2011-08-25T08:10:06.123
@Gareth: But 1st: you're about 5% away, and 2nd: you're forced to call your function 16 times in sequence. You can't count incoming emails, for example, this way. – user unknown – 2011-08-25T13:13:40.810
@Peter: I don't think that my solution depends on a PRNG with 48 bits internal state. The only number which really doesn't fit into a short is the number of tests, to test my code. The PRNG only needs to produce shorts. However, a PRNG with 16 bits of state, is of course, together with 16 bits for a counter, 32 bit. – user unknown – 2011-08-25T13:17:20.600
@user unknown, you're only asking the PRNG for 16 bits at a time, but I bet it uses more than that internally because otherwise its cycle would be so short that it would be seriously deficient. In fact, since it's Scala you're using Java's PRNG, which is documented to have 48 bits of internal state.
– Peter Taylor – 2011-08-25T13:29:15.167Not deficient for this task. – user unknown – 2011-08-25T13:34:28.903
@user unknown I am inaccurate, yes - but the question does say approximately and changing the loop comparison to use 62500 like everyone else fixes that problem. Yes, I am forced to do a contrived thing like calling a function 16 times in a row, but the whole question is contrived - it is a puzzle after all. – Gareth – 2011-08-25T13:40:00.080
2See 2.: You have to compose a function, and count, how often it is called. You're composing a function, and count another functions invocations (which isn't composed - it's just a name). – user unknown – 2011-08-25T13:56:52.983
1@Peter Taylor: Yeah, I had similar reservations about the random number generator. However, because the problem does not specify hardward requirements other than memory, I'd use a CPU with a built-in random number generator. I.E. It loads register 1 with x, register 2 with y, and then runs the random number generator instruction to load register 1 with a random number between x and y. This would carry the same weight as an addition, and if we can't add, what can we do? ;) – Briguy37 – 2011-08-25T14:15:32.390
@user unknown That's not the way I read 2. It says 'find a way to know when the function has run 1 million times'. I do that. It's not very useful because the million times are all in succession, but that was not in the restrictions. – Gareth – 2011-08-25T14:24:23.080
no love for this question :( – RHPT – 2011-09-02T15:32:12.820