Skip to content

Instantly share code, notes, and snippets.

@poizan42 poizan42/eratosthenes.cs
Last active Mar 14, 2016

Embed
What would you like to do?
Example showing the problem described in icsharpcode/ILSpy#684. Code originally written for [02242 Program Analysis](http://www.kurser.dtu.dk/02242.aspx), the language used is called While+ (but note that there are a couple of slightly different languages with that designation used in analysis and semantics litterature).
// Program
static int Main(string[] array)
{
int[] array2 = new int[1000];
int num = int.Parse(Console.ReadLine());
if ((num >= 1000) ?? (num < 2))
{
Console.WriteLine(-1);
}
else
{
int num2 = 2;
for (int num3 = 2; num3 <= num; num3 = num2)
{
Console.WriteLine(num3);
while (num2 <= num)
{
int num4 = 1;
array2[num2] = num4;
num2 += num3;
}
num2 = num3;
while (true)
{
bool arg_A9_0;
if (arg_A9_0 = (num2 <= num))
{
arg_A9_0 = (array2[num2] != 0);
}
if (!arg_A9_0)
{
break;
}
num2++;
}
}
}
return 0;
}
.method static privatescope
int32 Main$PST06000001 (
string[] ''
) cil managed
{
// Method begins at RVA 0x2050
// Code size 196 (0xc4)
.maxstack 11
.entrypoint
.locals init (
[0] int32,
[1] int32,
[2] int32,
[3] int32[],
[4] int32
)
IL_0000: ldc.i4 1000
IL_0005: newarr [mscorlib]System.Int32
IL_000a: stloc.3
IL_000b: call string [mscorlib]System.Console::ReadLine()
IL_0010: call int32 [mscorlib]System.Int32::Parse(string)
IL_0015: stloc.2
IL_0016: ldloc.2
IL_0017: ldc.i4 1000
IL_001c: clt
IL_001e: ldc.i4.0
IL_001f: ceq
IL_0021: dup
IL_0022: brtrue IL_0030
IL_0027: pop
IL_0028: ldloc.2
IL_0029: ldc.i4 2
IL_002e: clt
IL_0030: brfalse IL_0045
IL_0035: ldc.i4 1
IL_003a: neg
IL_003b: call void [mscorlib]System.Console::WriteLine(int32)
IL_0040: br IL_00c2
IL_0045: ldc.i4 2
IL_004a: stloc.0
IL_004b: ldc.i4 2
IL_0050: stloc.1
// loop start (head: IL_0051)
IL_0051: ldloc.1
IL_0052: ldloc.2
IL_0053: cgt
IL_0055: ldc.i4.0
IL_0056: ceq
IL_0058: brfalse IL_00c2
IL_005d: ldloc.1
IL_005e: call void [mscorlib]System.Console::WriteLine(int32)
// loop start (head: IL_0063)
IL_0063: ldloc.0
IL_0064: ldloc.2
IL_0065: cgt
IL_0067: ldc.i4.0
IL_0068: ceq
IL_006a: brfalse IL_0088
IL_006f: ldc.i4 1
IL_0074: stloc.s 4
IL_0076: ldloc.3
IL_0077: ldloc.0
IL_0078: ldloc.s 4
IL_007a: stelem.any [mscorlib]System.Int32
IL_007f: ldloc.0
IL_0080: ldloc.1
IL_0081: add
IL_0082: stloc.0
IL_0083: br IL_0063
// end loop
IL_0088: ldloc.1
IL_0089: stloc.0
// loop start (head: IL_008a)
IL_008a: ldloc.0
IL_008b: ldloc.2
IL_008c: cgt
IL_008e: ldc.i4.0
IL_008f: ceq
IL_0091: dup
IL_0092: brfalse IL_00a9
IL_0097: pop
IL_0098: ldloc.3
IL_0099: ldloc.0
IL_009a: ldelem.any [mscorlib]System.Int32
IL_009f: ldc.i4 0
IL_00a4: ceq
IL_00a6: ldc.i4.0
IL_00a7: ceq
IL_00a9: brfalse IL_00bb
IL_00ae: ldloc.0
IL_00af: ldc.i4 1
IL_00b4: add
IL_00b5: stloc.0
IL_00b6: br IL_008a
// end loop
IL_00bb: ldloc.0
IL_00bc: stloc.1
IL_00bd: br IL_0051
// end loop
IL_00c2: ldc.i4.0
IL_00c3: ret
} // end of method Program::Main
(* This is a more complicated example which implements the Sieve of Eratosthenes.
This is mostly for having a longer program to benchmark against. *)
program
int i;
int m;
int c;
int A[1000];
read c; (* Number of prime numbers to generate *)
if (c >= 1000) | (c < 2) then (* check bounds *)
write -1;
else
i := 2;
m := 2;
while m <= c do
write m;
(* Mark every multiplum of current m *)
while (i <= c) do
A[i] := 1;
i := i + m;
od
(* Search for first unmarked number starting from m *)
i := m;
while (i <= c) & (A[i] != 0) do
i := i + 1;
od
m := i; (* m is larger than c now if we are done, so the loop ends. *)
od
fi
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.