Re: More on triangle numbers and primes!



>> Okay, there is good news and bad news.
>>
>> The good news is that this is a fast and easy-to-code sieve for proving a
>> target number prime or not prime. The bad news is that it is slower than
>> an application based on a traditional or expected odd composite output...
>>
>> So, 999999999999989 was prime in about 5 seconds with variables set as
>> doubles. The application that this was compared to found all the primes
>> between target-number-minus-500 and target number in about 1 second.
>>
>> And, 9999999999999937 was prime in about 17 seconds with variables set as
>> doubles or prime in about 25 seconds with variables set as extended. The
>> application compared to found all the primes between
>> target-number-minus-500 and target number in about 5 seconds.
>>
>

Var
r, ct, ctt, n, st, nn, pt: double;
c: double;
flg: integer;

begin
{KBH Code}
Write(' Input n: ');
ReadLn(n);
flg:= 0;
If (Frac(n / 2) = 0) Then flg:= 1;
nn:= n + 1;
r:= 6;
ct:= 3;
ctt:= 3;
While (r < n) Do
Begin
r:= r + ctt;
If (r > n) Then Break;
c:= Trunc(n / ct) - Trunc(r / ct);
pt:= (c * ct) + r;
st:= pt;
While (st < nn) Do
Begin
If (st = n) Then flg:= 1;
st:= st + ct;
End;
If (flg = 1) Then Break;
ct:= ct + 1;
ctt:= ct + 1;
End;
WriteLn;
If (n > 10) Then
Begin
If (flg = 1) Then WriteLn(n:20:0, ' not prime') Else
WriteLn(n:20:0, ' is prime');
WriteLn;
End;
ReadLn;
end.


>> It just occurred to me that this code does not skip outer loops where
>> t(n) - 1 is even and the row spacing count is also even. Probably it's
>> just as fast to run those rows as it is to have additional logic in the
>> loops and thus more loop overhead...because the code just steps right to
>> the relevant location of the row.
>>
>> Finally, the code was run in Delphi console mode while the application
>> compared to is implemented in Delphi GUI mode...
>>
>

.