Skip to content

Instantly share code, notes, and snippets.

@dekajp
Created January 3, 2013 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekajp/4446769 to your computer and use it in GitHub Desktop.
Save dekajp/4446769 to your computer and use it in GitHub Desktop.
In his 1973 book Systematic Programming: An Introduction, Niklaus Wirth gives the following problem as exercise 15.12: Develop a program that generates in ascending order the least 100 numbers of the set M, where M is defined as follows: a) The number 1 is in M. b) If x is in M, then y = 2 * x + 1 and z = 3 * x + 1 are also in M. c) No other num…
//http://programmingpraxis.com/2012/12/07/wirth-problem-15-12/
namespace Wirth_Problem_15._12
{
class Program
{
static int MAX_SIZE = 10000;
static byte?[] aWrith = new byte?[MAX_SIZE];
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
int nextNumber = GetNext();
aWrith[nextNumber] = 1;
aWrith[nextNumber * 2 + 1] = 0;
aWrith[nextNumber * 3 + 1] = 0;
}
for (int i = 0; i < MAX_SIZE; i++)
{
if (aWrith[i] == 1)
{
System.Console.Write(String.Format("{0},", i));
}
}
}
static int GetNext()
{
for (int i = 1; i < MAX_SIZE; i++)
{
if (aWrith[i] == 0)
{
return i;
}
}
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment