Sujet : Re: while((i*i) < ps) {++i;}
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.os.linux.advocacyDate : 07. Mar 2024, 14:17:05
Autres entêtes
Message-ID : <65e9b041$0$2088294$882e4bbb@reader.netnews.com>
References : 1 2
User-Agent : Betterbird (Windows)
On 3/6/2024 9:41 PM, Relf wrote:
DFS:
Write your own C program with a single line of logic to determine the
root of a given [!perfect square], without using a sqrt() function.
>
My solution at:
http://s000.tinyupload.com/index.php?file_id=35742826989410139221
Your link doesn't work. 2^.5 == 1.41421354:
float Given = 2, SqrRt = 1 ; LoopJ(4) SqrRt = ( SqrRt + Given/SqrRt )/2 ;
// For context, see "z1.CPP" in "Jeff-Relf.Me/z1.ZIP".
That post was from Dec 2020. tinyupload.com went offline in May 2021 and never came back.
The code was:
------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
//triangular calc
int tri(int n) { return (n*(n+1))/2; }
//program timing
LARGE_INTEGER frequency,start,end;
double elapsedtime(LARGE_INTEGER startingtimer)
{
QueryPerformanceCounter(&end);
return (end.QuadPart - startingtimer.QuadPart) /
(double)frequency.QuadPart;
}
int main(int argc, char *argv[])
{
QueryPerformanceFrequency(&frequency);
int ps = atoi(argv[1]);
int loops = atoi(argv[2]);
int j = 1;
QueryPerformanceCounter(&start);
for(int t = 0;t < loops; t++)
{
while((tri(j-1) + tri(j)) < ps) {++j;}
}
printf(" %.4fs to loop %dx Feeb method (root =
%d)\n",elapsedtime(start), loops, j);
int i = 0;
QueryPerformanceCounter(&start);
for(int t = 0;t < loops; t++)
{
while((i*i) < ps) {++i;}
}
printf(" %.4fs to loop %dx DFS method (root =
%d)\n",elapsedtime(start), loops, i);
return(0);
}
---------------------------------------------------------------------
My method was 4x-5x faster than Feeb's.
$ psqr 60481729 10000000
0.2400s to loop 10000000x Feeb method (root = 7777)
0.0536s to loop 10000000x DFS method (root = 7777)