# Divisibility by seven

Given number n, whose decimal representation contains digits only $1, 6, 8, 9$. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.

If number is m digited after rearrangement it should be still $m$ digited.

If not possible then i need to tell "not possible".

EXAMPLE :

$1689$

After rearrangement we can have $1869$, which is divisible by $7$

How to tackle his problem

-
But 18906 contains also digit 0. –  user87690 Dec 24 '13 at 15:22
You write "digits only 1,6,8,9", don't you? –  mathlove Dec 24 '13 at 15:29
Are you asking for a method to do this for any such $n$? So for example if it is not possible for $1689$, would that constitute a proof that it cannot be done? (I'm not suggesting that it cannot be done for $1689$; haven't checked.) –  alex.jordan Dec 24 '13 at 15:33
@alex.jordan yeah...m asking for any such n..Not for just this number –  user3001932 Dec 24 '13 at 15:34
Well, it cannot be done with the number $1$, which technically meets your conditions. Must each digit appear at least once? –  alex.jordan Dec 24 '13 at 15:36

This is not a complete answer. But it will tell you some hints.

You should know the following algorithm :

For example, we know $35123473$ is a multiple of $7$ in the following way.

First, divide it as $35｜123｜473$, then add $35+473=508$ (odd sections), and add $123$ (even sections). And calculate $508-123=385$. Since $385$ is a multiple of $7$, $35123473$ is a multiple of $7$.

So, let us come back to the original question.

From the above algorithm, we know we only need to look at the set of a number in a section. The number in a section has at most three digits.

So, we now know we only need to look at the following numbers as a number in a section. $$1,6,8,9$$ $$11,16,18,19,66,68,69,88,89,99$$ $$111,666,888,999,168,169,189,689$$

By the way, when we look at them in mod $7$, we have $$1,6,1,2$$ $$4,2,4,5,3,5,6,4,5,1$$ $$6,1,6,5,0,1,0,3$$

I think you should find a good algorithm from this idea.

-

The number $N=a_n10^n+a_{n-1}10^{n-1}+a_{n-2}10^{n-2}+\cdots +10a_1+a_0$ is divisible by $7$ if and only if the number

$$(100a_0+10a_1+a_2)-(100a_5+10a_4+a_3)+(100a_8+10a_7+a_6)-\cdots$$

divisible by $7$.

(Idea for proving that is looking on $N$ in modulu $1001$)

-

If there is no condition that each of these digits need appear at least once, then it is not possible. Consider $1111$.

-
then i need to print 0.What the problem in it –  user3001932 Dec 24 '13 at 15:44

I created a rule for divisibility by seven, eleven and thirteen whose algorithm for divisibility by seven is this: N = a,bcd; a' ≣ ( − cd mod 7 + a ) mod 7; cd is eliminated and if 7|a'b then 7|N. The procedure is applied from right to left repetitively till the leftmost pair of digits is reached. If the leftmost pair is incomplete consider a = 0. Example: N = 382,536, using simple language: 36 to 42 = 6; 6 + 2 − 7 = 1 → 15; 15 to 21 = 6; 6 + 3 − 7 = 2 → 28; 7|28 and 7|N. This rule is mentioned in my unpublished (officially registered) book: Divisibility by 7, the end of a myth?.

-