blob: 10ac55350fe153f952dea64a8dd008981a1a5093 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
VampireNumber Java task, code explanation with the number 1827:
1827 (Number that we are checking)
0123 (Indexes of digits, in code they are named: first, second, third & fourth)
The first and second number that are multiplied are illustrated combined as one (the first half of the calculated numbers is the first multiple, and the second half - the second)
For example, the number 1278 is actually 12 and 78 and they are multiplied to check for the result
This is done like that to better illustrate how the numbers change
Cycle: Swap 1 with 3 (indexes):
1827 1728
1782 1287
1278 1872
2178 2871
2817 2718
2781 2187 NOTE: 2187 is the answer, so the code would stop here, but this example is continued to the end
7281 7182
7128 7821
7812 7218
8712 8217
8271 8172
8127 8721
1827 (Here the cycles end)
Cycle: it rotates the last 3 digits, for example if we have 123,the 3 gets in the front and the others move back, i.e. 312
1 2 3 => 3 1 2
^-----'
Swap 1 with 3: as the name suggests, it makes a second version of the number by swapping the 1st (index) and 2nd (index) digit
0 1 2 3 => 0 3 2 1
^---^
-the swap is there because with the cycle we miss out on half of the possible numbers
|