c++ – can anyone help me in this time exceeds error

Everything in this problem is known at compile time. So lets let the compiler solve this:

#include <stdio.h>
#include <cstddef>

consteval std::size_t solve() {
    int a=0;
    std::size_t count = 0;
    do {
        a = (a*10 +7) % 2003;
    } while (a != 0);
    return count;

int main() {
    printf("%zd\n", solve());

resulting in:

        .string "%zd\n"
        sub     rsp, 8
        mov     esi, 1001
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        call    printf
        xor     eax, eax
        add     rsp, 8

As may see this only calls printf with 1001 as argument.

Read more here: Source link