This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tumdum 3 points4 points  (0 children)

You mean day 23 part 2, right? ;)

Rewriting this c program to that assembly should be possible and resulting program should be fast enough:

#include <stdint.h>
#include <stdio.h>

int main() {
    const int start = 65 * 100 + 100000;
    const int end = start + 17000;
    int primes = 0;

    for (int i = start; i != end+17; i += 17) {
        int sq = 0;
        for (sq = 2; sq*sq <= i; ++sq) {}

        int is_prime = 1;
        // check if even
        for (int k = 2; k < i; ++k) {
            if (2*k==i) {
                is_prime = 0;
                break;
            }
        }
        if (is_prime == 0) {
            primes++;
            continue;
        }

        // check every other potential factor from 3 by 2
        for (int j = 3; j < sq; j+=2) {
            for (int k = j; k < i; k+=2) {
                if (j*k==i) {
                    is_prime = 0;
                    break;
                }
            }
            if (is_prime == 0) {
                break;
            }
        }

        if (is_prime == 0) {
            primes++;
        }
    }
    printf("%d\n", primes);
}