-4

** Consider the following sequence: 7,77,777, 7777,... Let T be the smallest element in this sequence that is divisible by 2003. How many digits does T have?** enter code here

#include <iostream>
#include <stdio.h>
using namespace std;
int main() 
{
    int a=0;
    int count =0;
    for(int i=1;;i++);
    {
        a=a*10+7;
    
        if (a%2003==0)
        {
            break;
        }
        else 
        {
            count++;;
        }
    }
    cout << count+1 ;
    return 0;
}
  • I cannot reproduce that error with your code, sorry. – πάντα ῥεῖ Jun 01 '22 at 15:34
  • My guess is your code overflows a 32 bit integer pretty quickly causing UB: [https://stackoverflow.com/questions/16188263/is-signed-integer-overflow-still-undefined-behavior-in-c](https://stackoverflow.com/questions/16188263/is-signed-integer-overflow-still-undefined-behavior-in-c) – drescherjm Jun 01 '22 at 15:34
  • Given the data being all 7's, and that modulus being 2003, all signs point to this being solved without having to write any loop, but instead some math "trick" to give you the answer immediately. Ask yourself why 7 and that seemingly "random" 2003 would appear in the question. Why not 2004, or 1674, or 5192, or some other value? – PaulMcKenzie Jun 01 '22 at 15:38
  • 2
    `for (int i = 1;; i++);` <- the semicolon at the end there is the culprit (or at least _one_ of them). This makes it an infinite loop you'll get signed integer overflow. Undefined behavior. – Ted Lyngmo Jun 01 '22 at 15:41
  • 1
    `for (int i = 1;; i++);` is an infinite loop that does nothing because of the semicolon. I didn't see before @TedLyngmo pointed out the semicolon problem. – drescherjm Jun 01 '22 at 15:44
  • 1
    Btw, why `for(int i=1;;i++)` when you never use `i`? Why not simply `for(;;)` ? – Ted Lyngmo Jun 01 '22 at 15:53
  • @PaulMcKenzie It's surely so that it causes integer overflows if you don't do modulo arithmetic. – Goswin von Brederlow Jun 01 '22 at 18:41

3 Answers3

1

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;
        ++count;
    } while (a != 0);
    return count;
}

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

resulting in:

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

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

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
0

If you print the value of a you can see that it overflows after a few iterations. Anyway the error is due to an infinite loop, so your process is killed.

nicFlower
  • 53
  • 7
-1

Try replacing the int with double.

MROJ4N
  • 1
  • 1
    I see three `int`s in the program and I can't even guess which one you mean or why you think that it would help. Can you elaborate? – Ted Lyngmo Jun 01 '22 at 15:50