Anonymous user
Mian-Chowla sequence: Difference between revisions
m
→{{header|Pascal}}: small improvement, known where to insert.Show runtime behavier of this implementation
(Added Kotlin) |
m (→{{header|Pascal}}: small improvement, known where to insert.Show runtime behavier of this implementation) |
||
Line 803:
{{works with|Free Pascal}}
keep sum of all sorted.Memorizing the compare positions speeds up.
<BR>
Calculating runtime to expect.
<pre>
n-> 2x n ~>
average distance between d = mian[n]-mian[n-1] d ~ 4x d
runtime t-> >10x t. ->
n mian[n] avg dist runtime
2700 225432491 195419 43411 ms
5400 1613654038 766839 471468 ms
</pre>
So calculating mian[25000] = mian[5940*(2**2,0733)]
runtime minimum > 10.86**2.0733 *471.5s = 66251 s and round about 2.5 Gbyte for Uint64.
One or two days should be enough.
<lang pascal>program MianChowla;
//compiling with /usr/lib/fpc/3.2.0/ppcx64.2 -MDelphi -O4 -al "%f"
{$CODEALIGN proc=8,loop=4 }
uses
sysutils;
const
maxCnt = 1000;
{
deltaK = 540;
maxCnt = (5966 DIV deltaK)*deltaK;
*}
type
tElem = Uint32;
tpElem =
t_n = array[0..maxCnt+1] of tElem;
t_n_sum_all = array[0..(maxCnt+1)*(maxCnt+2) DIV 2] of tElem;
var
n_LastPos,
n : t_n;
n_sum_all : t_n_sum_all;
maxIdx,
Line 832 ⟶ 853:
max_SumIdx := 1;
n_sum_all[max_SumIdx] := 2*maxN;
For i := 0 to maxCnt do
n_LastPos[i] := 1;
end;
procedure InsertNew_sum(NewValue:NativeUint);
//insertionsort
var
pElem :tpElem;
InsIdx,chkIdx,oldIdx,newIdx
Begin
newIdx := maxIdx;
oldIdx := max_SumIdx;
Line 849 ⟶ 871:
//extend sum_
inc(max_SumIdx,maxIdx);
//heighest value already known
InsIdx := max_SumIdx;
n_sum_all[InsIdx] := 2*NewValue;
//stop mark
n_sum_all[InsIdx+1] := High(tElem);
pElem := @n_sum_all[0];
dec(InsIdx);
//n_LastPos[newIdx]+newIdx-1 == InsIdx
repeat
//move old bigger values
chkIdx := n_LastPos[newIdx]+newIdx-1;
while InsIdx > chkIdx do
Begin
pElem
dec(InsIdx);
dec(oldIdx);
end;
dec(InsIdx);
dec(newIdx);
//all inserted
until newIdx <=
//new minimum search position one behind, oldidx is one to small
inc(oldidx,2);
For newIdx := 1 to maxIdx do
n_LastPos[newIdx] := oldIdx;
end;
procedure FindNew;
var
pSumAll,pn : tpElem;
i,LastCheckPos,newValue,newSum : NativeUint;
TestRes : boolean;
begin
//start value = last inserted value
pSumAll :=
pn := @n[0];
repeat
inc(newValue);
LastCheckPos := n_LastPos[1];
i := 1;
IF LastCheckPos < n_LastPos[i] then
LastCheckPos := n_LastPos[i];
while
inc(LastCheckPos);
n_LastPos[i] :=
TestRes:= pSumAll[LastCheckPos] = newSum;
IF TestRes then
BREAK;
until i>maxIdx;
//found?
If not(TestRes) then
BREAK;
until false;
InsertNew_sum(
end;
var
i,k : NativeInt;
procedure Out_num(k:NativeInt);
Begin
T1 := GetTickCount64;
// k n[k] average dist last deltak total time
writeln(k:6,n[k]:12,(n[k]-n[k-deltaK+1]) DIV deltaK:8,T1-T0:8,' ms');
end;
BEGIN
writeln('Allocated memory ',2*SizeOf(t_n)+Sizeof(t_n_sum_all));
T0 := GetTickCount64;
while t0 = GetTickCount64 do;
T0 := GetTickCount64;
Init;
k :=
i := 1;
repeat
repeat
FindNew;
inc(i);
until i=k;
Out_num(k);
k := k+deltaK;
until k>maxCnt;
writeln;
writeln(#13,'The first 30 terms of the Mian-Chowla sequence are');
For i := 1 to 30 do
Line 918 ⟶ 969:
writeln;
writeln;
writeln('The terms
For i :=
write(n[i],' ');
writeln;
END.
{#####
deltaK = 540;
maxCnt = (5966 DIV deltaK)*deltaK;
Allocated memory 70650384
540 2574622 4767 234 ms
1080 17315247 27269 2084 ms
1620 53808559 67542 7876 ms
2160 119743492 121877 20367 ms
2700 225432491 195419 43411 ms
3240 377659559 281283 80801 ms
3780 585851493 384664 137472 ms
4320 853722843 494787 217026 ms
4860 1198603744 637443 328136 ms
5400 1613654038 766839 471468 ms
5940 2124053407 944624 659681 ms
real
}
</lang>
{{Out}}
<pre>Allocated memory 2014024
100 27219 272 0.002 s
200 172922 1443 0.011 s
300 514644 3404 0.037 s
400 1144080 6197 0.090 s
500 2085045 9398 0.179 s
600 3375910 12689 0.311 s
700 5253584 18705 0.520 s
800 7600544 23438 0.801 s
900 10441056 28339 1.160 s
1000 14018951 35611 1.640 s
The first 30 terms of the Mian-Chowla sequence are
1 2 4 8 13 21 31 45 66 81 97 123 148 182 204 252 290 361 401 475 565 593 662 775 822 916 970 1016 1159 1312
The terms
=={{header|Perl}}==
|