I ran into an interesting problem the other day, someone asked for all positive integers of i & j, provide an ordered list of the solutions to (2^i)(5^j). After wracking my brains for quite a couple hours, I finally came up with the following solution. It work by finding integers for which there is an i and j in a sieve fashion. This works by solving for i, such that i = log2 (potential / (5^j)). This will allow the program to output as many values as the representation can support. I limit the number of values of j that I test for by limiting it to 5^j < potential. This gives the program O(n).
class Program
{
static void Main()
{
int potential = 0;
do
{
if (ExistsIandJ(potential))
Console.WriteLine("{0}", potential);
potential++;
} while (potential <= int.MaxValue)
}
private static bool ExistsIandJ(int potential)
{
// potential = (2^i)*(5^j)
// 1 = (2^i)*(5^j)/potential
// 1/(2^1) = (5^j)/potential or (2^i) = potential / (5^j)
// i = log2 (potential / (5^j))
for (var j = 0; Math.Pow(5,j) <= potential; j++)
{
var i = Math.Log(potential / Math.Pow(5, j), 2);
if (i == Math.Truncate(i))
return true;
}
return false;
}
}
I was expecting this to be fairly easy to begin with, the realized that wasn't going to be true. I then thought there must be a pattern to the values to i & j, but after a while realized there was not. Trees might be the answer, but only for a finite number of values, since you would have to determine when to prune the tree. Would you miss an answer. It was later that I realized that if I tested every answer for an i & j that it could be done infinitely. I pondered how to determine i & j, then decided to solve for i. The pieces fell into place.
Wednesday, July 29, 2015
Saturday, February 11, 2012
A few months ago, I started writing a game for the Android. It is a simple game inspired from an issue of 80 Micro. The game published in that magazine was for the TRS-80 Model I. Its objective was to pilot a space ship through a field of stars to land on a planet. The stars didn't move and the ship didn't move in real-time. It was a cool game none-the-less. My version of the game implements a simple version of gravity so that the ship is continuously falling toward the planet, and the stars are moving -- each in a different direction and rate.
To the right is an early snapshot of the game. The ship is at the top and the stars are sprinkled randomly across the screen. In the forthcoming posts I'm going to talk about the development of this game and how it is progressing.
To the right is an early snapshot of the game. The ship is at the top and the stars are sprinkled randomly across the screen. In the forthcoming posts I'm going to talk about the development of this game and how it is progressing.
Subscribe to:
Posts (Atom)