/* find and return the score of the window in the backgroundData that encloses * and is nearest to position */ bgPoint* getNearestEnclosing(long position, bgPoint* backgroundData, long numberOfWindows, long maxRadius) { bgPoint* current; long minDistance = LONG_MAX; bgPoint* minWindow = 0; current = findUpperBound(position + maxRadius, backgroundData, numberOfWindows); while(current->position >= position - maxRadius && current >= backgroundData) { /* if the current window contains the point */ if(current->position - current->radius <= position && position <= current->position + current->radius) { /* check to see if it is closer than the current minimum */ if(labs(current->position - position) < minDistance) { minDistance = labs(current->position - position); minWindow = current; } } --current; } return minWindow; }
long solution() { auto upperBound = findUpperBound(); unsigned long sum = 0; for(unsigned long num = 10; num <= upperBound; ++num){ if(sumPowerDigits(num) == num){ sum += num; } } return sum; }
char* CompiledLoop::recognize() { // Recognize integer loop. // We're looking for a loop where the loopVariable is initialized just before // the loop starts, is defined only once in the loop (increment/decrement), // and the loop condition is a comparison against a loop invariant. discoverLoopNesting(); if (!OptimizeIntegerLoops) return "!OptimizeIntegerLoops"; char* msg; if ((msg = findUpperBound()) != NULL) return msg; if ((msg = checkLoopVar()) != NULL) return msg; if ((msg = checkUpperBound()) != NULL) return msg; _isIntegerLoop = true; findLowerBound(); // don't need to know to optimize loop; result may be non-NULL return NULL; }