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

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. πŸ” Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...