IBM Ponder This Jan'22: Backtracking, Miller-Rabin and Caches

IBM Ponder This challenges take place once a month. It has been going on for many years. Most of the challenges, IMO, are really hard and I can't solve them. Every month I do take a look to see if it is solvable, and last month was a doable challenge.

Since I caught covid last month, and it hit me really hard, I stayed in isolation for several days. During that time I decided to take a crack at this problem. The problem is listed down below.

The way that I approached the problem was the following:
1/ Brute-force. Using backtracking to generate all the possible candidates. This is a standard Depth-First-Search (DFS) backtracking with search space pruning. It will take several minutes since there are just too many candidates. For the "*" bonus question, it does take several hours (~9h), so you need to run it overnight (standard average laptop)
2/ For the primarily testing, I'm a big fan of Miller-Rabin primality test: Miller–Rabin primality test - Wikipedia. The test is non-deterministic, it relies on the idea of using some "witness numbers" to "prove" that a certain number is prime. Although it is a non-deterministic model, the practical accuracy of this test approaches 100%, and its speed is incredible: ~LogN
3/ Even using Miller-Rabin primality test, since this test will be called millions of times, there is a need to cache the primes. The cache can be done inside the Miller-Rabin test itself.

With that, the execution time takes about ~20 minutes for the normal question, and ~9h for the bonus question. Code is here: IBMPonderThisJan2022/Program.cs at main · marcelodebarros/IBMPonderThisJan2022 (github.com)

Cheers, ACC



Forming primes in digit circles
The following riddle was suggested by Evert van Dijken.

Seven distinct digits are places on a circle. Then, numbers can be formed in the following manner: Choose one digit to start from and add it to the number, and then move a certain number of steps on the circle either clockwise or counterclockwise. Add the digit to the number, and keep this until five digits were chosen. These digits comprise the number (the first digit being the most significant digit).
For example, in the circle [4, 7, 3, 6, 2, 0, 1]:

One can start from "2", move 3 steps clockwise to "4", move one step counterclockwise to "1", move one more step counterclockwise to "0", and move 4 steps clockwise to "3", resulting in the number "24103".

Given a circle, we assign a score to each number that can be generated from the circle based on the minimum number of steps required to generate this number. In the example above, moving from "0" to "3" in 4 steps is not optimal; one can move just 3 steps by going counterclockwise. So the score of "24103" is 3+1+1+3=8.

We allow generation only of the numbers that satisfy all the following criterions:
1. Prime numbers.
2. All the digits are distinct.
3. No leading 0.

For a given circle, we can form all the numbers that satisfy the criterion and sum their scores.
For the circle [4, 7, 3, 6, 2, 0, 1] we will find 231 total primes with 5 distinct digits, giving us a total score of 1882. This 1882 is the score of the circle itself.

In our example we had n=7 digits in a circle and the numbers had d=5 digits but we can also generalize for different values of n,d (if n > 10 , we can use a different number base, but we won't do it here).

Your goal: Find circles giving the maximum and minimum score for the original n=7, d=5 setting.
Send your result in the following format:
[a1, a2, ..., an]
[b1, b2, ..., bn]

A bonus "*" will be given for finding circles giving the maximum and minimum score for n=8, d=6 .

Comments

Popular posts from this blog

Changing the root of a binary tree

ProjectEuler Problem 719 (some hints, but no spoilers)

Hard Leetcode Problem: Grid Illumination