Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
57
Task/Hailstone-sequence/Delphi/hailstone-sequence-1.delphi
Normal file
57
Task/Hailstone-sequence/Delphi/hailstone-sequence-1.delphi
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
program ShowHailstoneSequence;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses SysUtils, Generics.Collections;
|
||||
|
||||
procedure GetHailstoneSequence(aStartingNumber: Integer; aHailstoneList: TList<Integer>);
|
||||
var
|
||||
n: Integer;
|
||||
begin
|
||||
aHailstoneList.Clear;
|
||||
aHailstoneList.Add(aStartingNumber);
|
||||
n := aStartingNumber;
|
||||
|
||||
while n <> 1 do
|
||||
begin
|
||||
if Odd(n) then
|
||||
n := (3 * n) + 1
|
||||
else
|
||||
n := n div 2;
|
||||
aHailstoneList.Add(n);
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
i: Integer;
|
||||
lList: TList<Integer>;
|
||||
lMaxSequence: Integer;
|
||||
lMaxLength: Integer;
|
||||
begin
|
||||
lList := TList<Integer>.Create;
|
||||
try
|
||||
GetHailstoneSequence(27, lList);
|
||||
Writeln(Format('27: %d elements', [lList.Count]));
|
||||
Writeln(Format('[%d,%d,%d,%d ... %d,%d,%d,%d]',
|
||||
[lList[0], lList[1], lList[2], lList[3],
|
||||
lList[lList.Count - 4], lList[lList.Count - 3], lList[lList.Count - 2], lList[lList.Count - 1]]));
|
||||
Writeln;
|
||||
|
||||
lMaxSequence := 0;
|
||||
lMaxLength := 0;
|
||||
for i := 1 to 100000 do
|
||||
begin
|
||||
GetHailstoneSequence(i, lList);
|
||||
if lList.Count > lMaxLength then
|
||||
begin
|
||||
lMaxSequence := i;
|
||||
lMaxLength := lList.Count;
|
||||
end;
|
||||
end;
|
||||
Writeln(Format('Longest sequence under 100,000: %d with %d elements', [lMaxSequence, lMaxLength]));
|
||||
finally
|
||||
lList.Free;
|
||||
end;
|
||||
|
||||
Readln;
|
||||
end.
|
||||
51
Task/Hailstone-sequence/Delphi/hailstone-sequence-2.delphi
Normal file
51
Task/Hailstone-sequence/Delphi/hailstone-sequence-2.delphi
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
program ShowHailstoneSequence;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Types,
|
||||
System.Threading,
|
||||
System.SyncObjs,
|
||||
Boost.Algorithm,
|
||||
Boost.Int,
|
||||
System.Diagnostics;
|
||||
|
||||
var
|
||||
lList: TIntegerDynArray;
|
||||
lMaxSequence, lMaxLength, i: Integer;
|
||||
StopWatch: TStopwatch;
|
||||
|
||||
begin
|
||||
lList := Hailstone(27);
|
||||
Writeln(Format('27: %d elements', [lList.Count]));
|
||||
Writeln(lList.toString(4), #10);
|
||||
|
||||
lMaxSequence := 0;
|
||||
lMaxLength := 0;
|
||||
|
||||
StopWatch := TStopwatch.Create;
|
||||
StopWatch.Start;
|
||||
|
||||
TParallel.for (1, 1, 100000,
|
||||
procedure(idx: Integer)
|
||||
var
|
||||
lList: TIntegerDynArray;
|
||||
begin
|
||||
lList := Hailstone(idx);
|
||||
if lList.Count > lMaxLength then
|
||||
begin
|
||||
TInterlocked.Exchange(lMaxSequence, idx);
|
||||
TInterlocked.Exchange(lMaxLength, lList.Count);
|
||||
end;
|
||||
end);
|
||||
|
||||
StopWatch.Stop;
|
||||
|
||||
Write(Format('Longest sequence under 100,000: %d with %d elements', [lMaxSequence,
|
||||
lMaxLength]));
|
||||
|
||||
Writeln(Format(' in %d ms', [StopWatch.ElapsedMilliseconds]));
|
||||
|
||||
Readln;
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue