-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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