Beta

Find Out the Number of Grains of Sand!

Description:

Problem Description

                          ,▄▀▀▀▀▀▀▀▄
                       ▄P▀     ⁿ '  ▀▄ ,,,
                    ,Æ▀          ▌]▄▀▀    ╙▀▄
                  ,▀'         ' ▄▀▌  ▌    ═,▐▀N▄,
                ▄▀"           ²  '        ▌¬ ▄╘ `▀▄
            ▄Æ▀└¬ ▐ ²                              ▀▄
          ▄▀    *  ⌐             ╒  ┌ ," ²   ┌       ▀▄
        ▄▀  ┐           ` ',▄▄▄▄,". /  ,ⁿ, ,▄▄mm▄,     ▀N▄
      ,█¬⌐   ═       ,"▄Æ▀'      `▀▄╓⌐  ▐▄▀-   MÆ;▀▄ ▌▄   ▀▄
     Æ└ ═ ^   ∞  `  ▄Æ▀─∞           ▀▀▄▀         ═  ▀N,▄  ▄▀
    ▌╙⌐ + '   ∞  ▄Æ▀',,▄ .           ⌐`▀▄ ∞           `▀▄▀
    ▀▄ ¬ *└⌐ ▄P▀¬ ╓      ╓`,         `╓  ▀▄╛ ∞*          "█
 ▄▀▌▀▀▀█▀N▄P▀ `        ,           ╒w      ▀█ " +"╓ª┌      █
█▄ * ▄A▀▄▀r"⌐ "⌐    "           ,            ▀▄═ ▄═▐,▄,▄▄▄▀└
  ª-, ▄▀═ ^;" ;  ▌   "         ,*▄`           ╙█▄▄█▄▄▄    "
       └▀▀▄', ∞ `   ⌐¬  ,  ,   └  ²       ═ ,▄Æ▀ ▀▄▄▄▌▄█
           ▀▀▄▓▄▓▄▄▄▄▄  '      "⌐ ⌐ ,▄mM▀▀▀█▀▀▀▄▄▄
         ▐██▌  +  -   ▐▀▀▀═MM∞∞∞MP▀▀ , "╒▀'`╤]▄▄▄A▀ 

This Kata is the second in the Do a Smart Guess collection series. If you haven't solved the first Kata, check it out: Find Out the Number of Gold Coins!.

After dealing with the guardian in the last Kata, you and your friends continue the RPG campaign, and now you encounter a mysterious wizard.

The wizard promises to give you a legendary magic item, but for that you must guess the number of grains of magic sand that are in the wizard's bag. For this, he gives the following tips:

  • 11. If the number of grains were divided between m1m_1 people, a1a_1 grains would be left.

  • 22. If the number of grains were divided between m2m_2 people, a2a_2 grains would be left.

  • 33. If the number of grains were divided between m3m_3 people, a3a_3 grains would be left.

    \vdots
  • NN. If the number of grains were divided between mNm_N people, aNa_N grains would be left.

Of course mim_i is always a positive integer and 0ai<mi0 \leq a_i \lt m_i.

The wizard doesn't seem very trustworthy, so he might be trying to trick you, and there aren't any number of grains of sand that satisfy the conditions he gives.

Your Task

Given the wizard information, write the function number_of_grains that returns the minimum number of grains of magic sand in the wizard's bag.

This function receives information in the form of a list of tuples:

[(a1, m1), (a2, m2), ... , (aN, mN)]

If the wizard is trying to trick you, and there is no number of grains of sand that satisfies the given information, your function should return None.

Example

Suppose that the tips from the wizard are:

  • 11. If the number of grains were divided between 44 people, 33 grains would be left.

  • 22. If the number of grains were divided between 66 people, 11 grain would be left.

So, the minimum number of grains of sand is 7, because 7 is the smallest number that when divided by 4 has remainder 3 and when divided by 6 has remainder 1. Thus:

number_of_grains([(3, 4), (1, 6)]) = 7

Suppose the wizard gave one more tip:

  • 33. If the number of grains were divided between 22 people, no grain would be left.

In this case, the problem has no solution, because there is no number that divided by 2 has a remainder of 0 (that is, an even number) that when divided by 4 has a remainder of 3. Thus:

number_of_grains([(3, 4), (1, 6), (0, 2)]) = None

Performance Requirements

Consider that the maximum input length will be 15 (that is, the wizard will give at most 15 tips, 1N151 \leq N \leq 15).

The mim_i values will be always less than 102010^{20}, so the minimum number of grains of sand can be quite large. However, that shouldn't be a problem for your algorithm.

Have fun coding and please don't forget to vote and rank this kata! :-)

Mathematics
Number Theory

More By Author:

Check out these other kata created by davimoreno

Stats:

CreatedMar 10, 2023
PublishedMar 10, 2023
Warriors Trained251
Total Skips119
Total Code Submissions62
Total Times Completed15
Python Completions15
Total Stars8
% of votes with a positive feedback rating100% of 7
Total "Very Satisfied" Votes7
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • davimoreno Avatar
Ad