[C#] 纯文本查看 复制代码
using System.Threading.Tasks;
class MainConWin32
{
private uint Sum = 0;
void Main()
{
private const string vbCrLf = "\n";
Action serialFor = () =>
{
Console.WriteLine(new string(vbCrLf + "[" + Now + "] Serial Operation..."));
for (uint Number = 2; Number <= uint.MaxValue; Number += 1) {
for (uint Div = 1; Div <= Number - 1; Div += 1) {
if (Number % Div == 0) {
Sum = Sum + Div;
}
}
if (Sum == Number)
Console.WriteLine(Number + Space(3) + "Time=" + TimeOfDay);
Sum = 0;
}
};
Action parallelFor = () =>
{
Console.WriteLine(new string(vbCrLf + "[" + Now + "] Parallel Operation..."));
Parallel.For(2, uint.MaxValue + 1, index =>
{
Parallel.For(1, index, s_index =>
{
if (index % s_index == 0) {
Sum = Sum + s_index;
}
});
if (Sum == index)
Console.WriteLine(index + " " + "Time=" + DateTime.TimeOfDay);
Sum = 0;
});
};
Console.WriteLine(new string("找完数" + vbCrLf));
Console.Write(new string("请选择执行操作的方式 ( 0=并行计算,1=串行计算 ):"));
char readChar = Convert.ToInt32(Console.Read(), 10);
if (readChar == "0") {
Task.Factory.StartNew(parallelFor).Wait();
} else if (readChar == "1") {
Task.Factory.StartNew(serialFor).Wait();
} else {
Console.WriteLine(new string(vbCrLf + "选择错误,默认将执行串行操作!!!"));
Task.Factory.StartNew(serialFor).Wait();
}
}
}
[Visual Basic .NET] 纯文本查看 复制代码
Imports System.Threading.Tasks
Module MainConWin32
Private Sum As UInteger = 0
Sub Main()
Dim serialFor As Action = Sub()
Console.WriteLine(New String(vbCrLf & "[" & Now & "] Serial Operation..."))
For Number As UInteger = 2 To UInteger.MaxValue Step 1
For Div As UInteger = 1 To Number - 1 Step 1
If Number Mod Div = 0 Then
Sum = Sum + Div
End If
Next Div
If Sum = Number Then Console.WriteLine(Number & Space(3) & "Time=" & TimeOfDay)
Sum = 0
Next Number
End Sub
Dim parallelFor As Action = Sub()
Console.WriteLine(New String(vbCrLf & "[" & Now & "] Parallel Operation..."))
Parallel.For(2, UInteger.MaxValue + 1, _
Sub(index)
Parallel.For(1, index, Sub(s_index)
If index Mod s_index = 0 Then
Sum = Sum + s_index
End If
End Sub)
If Sum = index Then Console.WriteLine(index & Space(3) & "Time=" & TimeOfDay)
Sum = 0
End Sub)
End Sub
Console.WriteLine(New String("找完数" & vbCrLf))
Console.Write(New String("请选择执行操作的方式 ( 0=并行计算,1=串行计算 ):"))
Dim readChar As Char = ChrW(Console.Read)
If readChar = "0" Then
Task.Factory.StartNew(parallelFor).Wait()
ElseIf readChar = "1" Then
Task.Factory.StartNew(serialFor).Wait()
Else
Console.WriteLine(New String(vbCrLf & "选择错误,默认将执行串行操作!!!"))
Task.Factory.StartNew(serialFor).Wait()
End If
End Sub
End Module