#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <inttypes.h> #include <time.h> typedef uint64_t U64; struct timespec diff_time( struct timespec end, struct timespec start ) { struct timespec res; res.tv_sec = end.tv_sec - start.tv_sec; res.tv_nsec = end.tv_nsec - start.tv_nsec; if ( res.tv_nsec < 0 ) { res.tv_nsec += 1000 * 1000 * 1000; res.tv_sec -= 1; } return res; } U64 gcd( U64 a, U64 b ) { return !b ? a : gcd( b, a % b ); } U64 lcm( U64 a, U64 b ) { return a * b / gcd( a, b ); } U64 gcm( U64 max, U64 n ) { U64 c, lcm; for( c = 1; ( ~n & 1 ) && ( c < 4096 ); c <<= 1 ) n >>= 1; lcm = n * 4096; return ( max / lcm ) * lcm; } #define GB ( 1024ull * 1024ull * 1024ull ) #define PAGE 4096 int main(int argc, char **argv) { U64 r, chk; U64 s = argc > 1 ? strtoull( argv[1], NULL, 10 ) : GB; struct timespec start, end, difft; clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &start ); for( chk = 0, r = 1; r < ( s >> 1 ); ++r ) { chk += gcm( s, r ); } clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &end ); difft = diff_time( end, start ); printf( "anonyM: gcm for s=%" PRIu64 " & r=1 to %" PRIu64 " took:%lld secs %ld nsecs\n" "chk = %" PRIu64 "\n", s, s>>1, (long long) difft.tv_sec, difft.tv_nsec, chk ); clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &start ); for( chk = 0, r = 1; r < ( s >> 1 ); ++r ) { chk += s - ( s % lcm( r, 4096 ) ); } clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &end ); difft = diff_time( end, start ); printf( "oiskuu: gcm for s=%" PRIu64 " & r=1 to %" PRIu64 " took:%lld secs %ld nsecs\n" "chk = %" PRIu64 "\n", s, s>>1, (long long) difft.tv_sec, difft.tv_nsec, chk ); return 0; }
In reply to Re^2: Simple arithmetic? (And the winner is ... )
by Anonymous Monk
in thread Simple arithmetic?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |