Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 85

C Exercise 85

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Enter a prime number and find the minimum number of consecutive digit 9s needed to form a positive integer divisible by that prime.

For example, enter the prime number 13:

Therefore, the output is 6, meaning that at least 6 consecutive digit 9s are needed.

Program analysis:

The key to this problem is to determine whether the integer formed by consecutive digit 9s is divisible by the given prime.

The most straightforward method is to start from 9 and add a 9 at the end each time, successively checking whether 9, 99, 999, 9999... are divisible by the prime.

However, as the number of digit 9s increases, the resulting integer grows larger and may easily exceed the representable range of C integer types.

Therefore, we can usethe modulo operationto solve this problem without having to store the complete integer.

Suppose the remainder of the current number divided by the prime p is r. After appending a 9 at the end, the remainder of the new number is:

r = (r * 10 + 9) % p;

Repeat the calculation until the remainder is 0; at that point, the number of digit 9s is the desired result.

**Note:** If the input prime is 2 or 5, no such number exists, because every positive integer made up of the digit 9 ends in 9 and cannot be divisible by 2 or 5.

Program source code:

Example

#include

// Determine whether an integer is prime

int

is_prime

(

int

n

)

{

   

if

(

n

<

2

)

{

return

0

;

   

}

   

for

(

int

i

=

2

;

i

<=

n

/

i

;

i

++

)

{

       

if

(

n

%

i

==

0

)

{

           

return

0

;

       

}

   

}

   

return

1

;

}

int

main

(

)

{

   

int

p

;

   

long

long

remainder

=

0

;

   

printf

(

"Please enter a prime number:\n"

)

;

   

if

(

scanf

(

"%d"

,

&

p

)

!=

1

)

{

       

printf

(

"Invalid input!\n"

)

;

       

return

1

;

   

}

   

// Verify whether the input is prime

   

if

(

!

is_prime

(

p

)

)

{

       

printf

(

"%d is not a prime number!\n"

,

p

)

;

       

return

1

;

   

}

   

// 2 and 5 cannot divide any positive integer made up only of the digit 9

   

if

(

p

==

2

||

p

==

5

)

{

       

printf

(

"There is no number made of consecutive 9s that is divisible by %d.\n"

,

p

)

;

       

return

0

;

   

}

   

// Compute using the modulo method to avoid large-integer overflow

   

for

(

int

count

=

1

;

count

<=

p

;

count

++

)

{

        remainder

=

(

remainder

10

9

)

%

p

;

       

if

(

remainder

==

0

)

{

           

printf

(

"At least %d digits of 9 are needed to form a number divisible by %d.\n"

,

                   count

,

p

)

;

           

return

0

;

       

}

   

}

   

return

0

;

}

The above example produces the following output:

请输入一个素数:

13

最少需要 6 个9组成的数才能被 13 整除。

Code explanation:

Algorithm description:

For any prime p other than 2 and 5, since p is coprime with 10, number theory guarantees that there exists a positive integer made entirely of the digit 9 that is divisible by p. Therefore, the loop needs at most p checks to find the result.

This algorithm does not need to store the complete integer. Its space complexity is O(1), and the search performs at most O(p) modulo operations.

**Tip:** This example only computes the minimum number of digit 9s needed; it does not directly output the complete integer formed by those digits. Therefore, even if the result contains a large number of digit 9s, no overflow occurs from constructing a big integer.

100 Classic C Language Examples 100 Classic C Language Examples