public
Created

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 numbers are in M. Wirth also gives the first six numbers in the result sequence: 1, 3, 4, 7, 9, 10…. Your task is to write a program that finds the first n numbers in Wirth’s sequence, and use it to solve Wirth’s exercise. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below. http://programmingpraxis.com/2012/12/07/wirth-problem-15-12/

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
//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;
}
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.