Home > OS >  Counting the number of "Nines", "Threes" and "Ones" in an integer betw
Counting the number of "Nines", "Threes" and "Ones" in an integer betw

Time:10-11

I came across a problem and I was unable to find a solution for it over the net. Maybe I wasn't entering in the right words for the accurate search. However, the problem goes like this:

Find how many of Nines, Threes and Ones it would take when added together to reach the given integer. When also multiplied by 1, 3 and 9.

In other words, say n = 25, we will then have 2 (Nines), 2 (Threes) and 1 (Ones). When we multiply them

2 * 9 = 18

2 * 3 = 6

1 * 1 = 1

total = 18 6 1 = 25 = n.

I was able to find a solution; however I was wondering if there's a smaller version of the same.

Here's my code.

def one_3_9(num):
    temp = num
    threes = []
    ones = []
    nines = []
    val_ = 0

    while temp != 0:
        if 0 < num <=8:
           if num >= 3:
               val_ = num // 3
               threes.append(val_)
               temp = temp - (3 * val_)
               ones.append(temp)
               temp = temp - temp
           elif num < 3:
               ones.append(num)
               temp = temp - temp

        if 26 >= num >= 9:
            val_ = num // 9
            nines.append(val_)
            temp = temp - (9 * val_)
            ones.append(temp)
            temp = temp - temp
            if temp >= 3:
                num = temp
                val_ = num // 3
                threes.append(val_)
                temp = temp - (3 * val_)
                ones.append(temp)
                temp = temp - temp
       else:
           print("Number Should be more than 0 and less than 26.")
           break
   print("threes: ", threes)
   print("Ones : ", ones)
   print("nines: ", nines)

I completely AGREE the answer is long-winded and perhaps so is my explanation. However, I really hope I make sense with this example.

Thank you.

CodePudding user response:

I came across a problem and I was unable to find a solution for it over the net. Maybe I wasn't entering in the right words for the accurate search. However, the problem goes like this:

Find how many of Nines, Threes and Ones it would take when added together to reach the given integer. When also multiplied by 1, 3 and 9.

In other words, say n = 25, we will then have 2 (Nines), 2 (Threes) and 1 (Ones). When we multiply them

2 * 9 = 18

2 * 3 = 6

1 * 1 = 1

total = 18 6 1 = 25 = n.

I was able to find a solution; however I was wondering if there's a smaller version of the same.

Here's my code.

def one_3_9(num):
    temp = num
    threes = []
    ones = []
    nines = []
    val_ = 0

    while temp != 0:
        if 0 < num <=8:
           if num >= 3:
               val_ = num // 3
               threes.append(val_)
               temp = temp - (3 * val_)
               ones.append(temp)
               temp = temp - temp
           elif num < 3:
               ones.append(num)
               temp = temp - temp

        if 26 >= num >= 9:
            val_ = num // 9
            nines.append(val_)
            temp = temp - (9 * val_)
            ones.append(temp)
            temp = temp - temp
            if temp >= 3:
                num = temp
                val_ = num // 3
                threes.append(val_)
                temp = temp - (3 * val_)
                ones.append(temp)
                temp = temp - temp
       else:
           print("Number Should be more than 0 and less than 26.")
           break
   print("threes: ", threes)
   print("Ones : ", ones)
   print("nines: ", nines)

I completely AGREE the answer is long-winded and perhaps so is my explanation. However, I really hope I make sense with this example.

Thank you.

CodePudding user response:

What you're describing is calculating the three digits of your number in base 3, assuming it's less than 27.

Here is some simpler code:

def one_3_9(num):
    nines = num // 9
    threes = (num%9) // 3
    ones = num % 3
    print("Nines:", nines)
    print("Threes:", threes)
    print("Ones:", ones)

CodePudding user response:

Loop through the values in descending order, use modulus division to find how many times it can go into the value and subtract that result from your running value:

def goes_into(n, vals):
    result = {}
    for i in sorted(vals, reverse=True):
        into = n // i
        result[i] = into
        n -= i * into
    if n % i:
        raise ValueError("n is not attainable by vals")
    return result

>>> goes_into(25, [1, 3, 9])
{9: 2, 3: 2, 1: 1}

CodePudding user response:

One of the approaches:

def calculate(num):
    nines = num//9
    threes = (num-(9*nines))//3
    ones = num - (9*nines) - (3*threes)
    print (f'{num} requires: 9s: {nines}, 3s: {threes}, 1s: {ones}')
    
calculate(49)
calculate(25)
calculate(37)

Output:

49 requires: 9s: 5, 3s: 1, 1s: 1
25 requires: 9s: 2, 3s: 2, 1s: 1
37 requires: 9s: 4, 3s: 0, 1s: 1
  • Related