Skip to content

# How to Calculate A Rule of Divisibility by 7 in C

## The challenge

A number m of the form 10x + y is divisible by 7 if and only if x − 2y is divisible by 7. In other words, subtract twice the last digit from the number formed by the remaining digits. Continue to do this until a number known to be divisible by 7 is obtained; you can stop when this number has at most 2 digits because you are supposed to know if a number of at most 2 digits is divisible by 7 or not.

The original number is divisible by 7 if and only if the last number obtained using this procedure is divisible by 7.

Examples:

1 – `m = 371 -> 37 − (2×1) -> 37 − 2 = 35` ; thus, since 35 is divisible by 7, 371 is divisible by 7.

The number of steps to get the result is `1`.

2 – `m = 1603 -> 160 - (2 x 3) -> 154 -> 15 - 8 = 7` and 7 is divisible by 7.

3 – `m = 372 -> 37 − (2×2) -> 37 − 4 = 33` ; thus, since 33 is not divisible by 7, 372 is not divisible by 7.

4 – `m = 477557101->47755708->4775554->477547->47740->4774->469->28` and 28 is divisible by 7, so is 477557101. The number of steps is 7.

Task:

Your task is to return to the function `seven(m)` (m integer >= 0) an array (or a pair, depending on the language) of numbers, the first being the last number `m` with at most 2 digits obtained by your function (this last `m` will be divisible or not by 7), the second one being the number of steps to get the result.

Forth Note:

See also  How to Get Last Digit of a Large Number in C

Return on the stack `number-of-steps, last-number-m-with-at-most-2-digits`

Examples:

```.wp-block-code {
border: 0;
padding: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
padding-left: 0.75em;
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
padding: 0 0.75em;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```seven(371) should return [35, 1]
seven(1603) should return [7, 2]
seven(477557101) should return [28, 7]```Code language: plaintext (plaintext)```

## The solution in C

Option 1:

``````#include <stdlib.h>

int* seven (long long m) {
int* res = (int*)malloc (2 * sizeof (int));

int step = ;

while (m > 99) {
m = m / 10 - 2 * (m % 10);
++step;
}

res[] = m;
res[1] = step;

return res;
}```Code language: C++ (cpp)```

Option 2:

``````int* seven(long long m) {
static int res[2] = {};
int cnt = ;
while (m > 99) {
long long a0 = m % 10;
m = (m - a0) / 10 - 2 * a0;
cnt++;
}
res[] = (int) m;
res[1] = cnt;
return res;
}```Code language: C++ (cpp)```

Option 3:

``````#include <assert.h>
#include <stdlib.h>

int* seven(long long n) {
assert(n >= );
int *result = (int *) malloc(2 * sizeof(int));
if (! result)
return NULL;
int steps = ;
for (; n > 99; ++steps)
n = n / 10 - n % 10 * 2;
result[] = n;
result[1] = steps;
return result;
}```Code language: C++ (cpp)```

## Test cases to validate our solution

``````#include <criterion/criterion.h>

extern int *seven (long long m);

static void do_test (long long m, const int expected[2])
{
int *actual = seven(m);

cr_assert_arr_eq(actual, expected, 2 * sizeof *expected,
"for m = %lld, expected {%d, %d} but got {%d, %d}",
m, expected[], expected[1], actual[], actual[1]
);
}

#define ARRAY (const int[2])

Test(tests_suite, fixed_tests)
{
do_test(1021, ARRAY{10, 2});
do_test(477557101, ARRAY{28, 7});
do_test(477557102, ARRAY{47, 7});
do_test(1603, ARRAY{7, 2});
do_test(371, ARRAY{35, 1});
do_test(1369851, ARRAY{, 5});
do_test(483, ARRAY{42, 1});
do_test(483595, ARRAY{28, 4});
do_test(, ARRAY{, });
do_test(170232833617690725ll, ARRAY {, 16});
}```Code language: C++ (cpp)```

See also  How to Return the Closest Number Multiple of 10 in C
Tags:
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x