#include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <time.h> #ifdef ENABLE_LOG void printFormatted(const char* format, ...); #define LOG(TAG, ...) {\ char buffer[256];\ time_t now = time(0);\ struct tm* timeVal = gmtime(&now);\ strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeVal);\ printFormatted("%s| %s:%d: %s ", buffer, __FILE__, __LINE__, TAG);\ printFormatted(__VA_ARGS__);\ printFormatted("\n");\ } FILE* initLogIfNecessary() { static FILE* fp = NULL; if(fp == NULL) { fp=fopen("log.txt", "a"); if(fp == NULL) { fprintf(stderr, ("Unable to open log file.\n")); return NULL; } printFormatted("Compiled on %s %s\n", __DATE__, __TIME__); } return fp; } void printFormatted(const char * format, ... ) { FILE* logFile = initLogIfNecessary(); va_list args; va_start (args, format); vfprintf (logFile, format, args); va_end (args); fflush(logFile); } #else #define LOG(TAG, ...) #endif unsigned long long squares[10]; void setSquareValue(char c, unsigned long long value) { int i = c - '0'; squares[i] = value; } int getSquareValue(char c) { int i = c - '0'; return squares[i]; } void initSquares() { setSquareValue('0', 0); setSquareValue('1', 1); setSquareValue('2', 4); setSquareValue('3', 9); setSquareValue('4', 16); setSquareValue('5', 25); setSquareValue('6', 36); setSquareValue('7', 49); setSquareValue('8', 64); setSquareValue('9', 81); } void helloWorldFunction() { LOG(__FUNCTION__, "HelloWorld\n"); } unsigned long long suchNumbersCount = 0; unsigned long long prefilteredCount= 0; void processCandidate(unsigned long long candidate, unsigned long long factor) { char buffer[32]; unsigned long long i = 0; memset(buffer, '0', sizeof(buffer)); sprintf(buffer, "%llu",candidate); unsigned long long squareSum = 0; char c = '\0'; do { c = buffer[i]; if(c >= '0' && c <= '9') { squareSum += getSquareValue(c); } i++; } while(c != '\0'); unsigned long long fN = squareSum * factor; LOG(__FUNCTION__, "f(n) = %llu",fN); if(squareSum * factor == candidate) { LOG(__FUNCTION__,"SuchNumber is : %llu",candidate); suchNumbersCount ++; } prefilteredCount ++; } void findFactors2(unsigned long long k, unsigned long long a, unsigned long long b) { LOG(__FUNCTION__, "(%llu,%llu,%llu)", k, a, b); unsigned long long i = 0; unsigned long long j = (a/k)*k; if(j != a) { j += k; } for (i = j; i <= b; i = i + k) { processCandidate(i, k); LOG(__FUNCTION__,"Candidate: %llu",i); } } void findFactors3(unsigned long long k, unsigned long long a, unsigned long long b) { unsigned long long i = 0; i = a; unsigned long long val; do { val = i%k; i++; } while(val != 0); a = i - 1; for (i = a; i <= b; i = i + k) { // if(i%k == 0) { processCandidate(i, k); LOG(__FUNCTION__,"Candidate: %llu",i); //} } } void findFactors(unsigned long long k, unsigned long long a, unsigned long long b) { unsigned long long i = 0; for (i = a; i <= b; i++) { // if(i%k == 0) { processCandidate(i, k); LOG(__FUNCTION__,"Candidate: %llu",i); //} } } #ifndef UNIT_TEST int main(int argc, char* argv) { initSquares(); unsigned long long k, a, b; scanf("%llu %llu %llu", &k, &a, &b); LOG(__FUNCTION__,"Read values: %llu, %llu, %llu", k, a, b); findFactors3(k,a,b); printf("%llu\n", suchNumbersCount); LOG(__FUNCTION__,"Total count is %llu, prefiltered = %llu",prefilteredCount,suchNumbersCount); } #endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <time.h> #ifdef ENABLE_LOG void printFormatted(const char* format, ...); #define LOG(TAG, ...) {\ char buffer[256];\ time_t now = time(0);\ struct tm* timeVal = gmtime(&now);\ strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeVal);\ printFormatted("%s| %s:%d: %s ", buffer, __FILE__, __LINE__, TAG);\ printFormatted(__VA_ARGS__);\ printFormatted("\n");\ } FILE* initLogIfNecessary() { static FILE* fp = NULL; if(fp == NULL) { fp=fopen("log.txt", "a"); if(fp == NULL) { fprintf(stderr, ("Unable to open log file.\n")); return NULL; } printFormatted("Compiled on %s %s\n", __DATE__, __TIME__); } return fp; } void printFormatted(const char * format, ... ) { FILE* logFile = initLogIfNecessary(); va_list args; va_start (args, format); vfprintf (logFile, format, args); va_end (args); fflush(logFile); } #else #define LOG(TAG, ...) #endif unsigned long long squares[10]; void setSquareValue(char c, unsigned long long value) { int i = c - '0'; squares[i] = value; } int getSquareValue(char c) { int i = c - '0'; return squares[i]; } void initSquares() { setSquareValue('0', 0); setSquareValue('1', 1); setSquareValue('2', 4); setSquareValue('3', 9); setSquareValue('4', 16); setSquareValue('5', 25); setSquareValue('6', 36); setSquareValue('7', 49); setSquareValue('8', 64); setSquareValue('9', 81); } void helloWorldFunction() { LOG(__FUNCTION__, "HelloWorld\n"); } unsigned long long suchNumbersCount = 0; unsigned long long prefilteredCount= 0; void processCandidate(unsigned long long candidate, unsigned long long factor) { char buffer[32]; unsigned long long i = 0; memset(buffer, '0', sizeof(buffer)); sprintf(buffer, "%llu",candidate); unsigned long long squareSum = 0; char c = '\0'; do { c = buffer[i]; if(c >= '0' && c <= '9') { squareSum += getSquareValue(c); } i++; } while(c != '\0'); unsigned long long fN = squareSum * factor; LOG(__FUNCTION__, "f(n) = %llu",fN); if(squareSum * factor == candidate) { LOG(__FUNCTION__,"SuchNumber is : %llu",candidate); suchNumbersCount ++; } prefilteredCount ++; } void findFactors2(unsigned long long k, unsigned long long a, unsigned long long b) { LOG(__FUNCTION__, "(%llu,%llu,%llu)", k, a, b); unsigned long long i = 0; unsigned long long j = (a/k)*k; if(j != a) { j += k; } for (i = j; i <= b; i = i + k) { processCandidate(i, k); LOG(__FUNCTION__,"Candidate: %llu",i); } } void findFactors3(unsigned long long k, unsigned long long a, unsigned long long b) { unsigned long long i = 0; i = a; unsigned long long val; do { val = i%k; i++; } while(val != 0); a = i - 1; for (i = a; i <= b; i = i + k) { // if(i%k == 0) { processCandidate(i, k); LOG(__FUNCTION__,"Candidate: %llu",i); //} } } void findFactors(unsigned long long k, unsigned long long a, unsigned long long b) { unsigned long long i = 0; for (i = a; i <= b; i++) { // if(i%k == 0) { processCandidate(i, k); LOG(__FUNCTION__,"Candidate: %llu",i); //} } } #ifndef UNIT_TEST int main(int argc, char* argv) { initSquares(); unsigned long long k, a, b; scanf("%llu %llu %llu", &k, &a, &b); LOG(__FUNCTION__,"Read values: %llu, %llu, %llu", k, a, b); findFactors3(k,a,b); printf("%llu\n", suchNumbersCount); LOG(__FUNCTION__,"Total count is %llu, prefiltered = %llu",prefilteredCount,suchNumbersCount); } #endif |