2

I'm trying to write a function that takes in two elements n and m and will try to give a count of the digits in n that are multiples of m.

For example, if n = 650899, m = 3, the answer is 4 because the digits {6, 0, 9, 9} are all evenly divisible by 3, while the digits {5, 8} are not:

Calculation         Cumulative count
----------------    ----------------
6 / 3 = 2                  1
5 / 3 = 1.666...
0 / 3 = 0                  2
8 / 3 = 2.666...
9 / 3 = 3                  3
9 / 3 = 3                  4 <- Answer

I'm trying to do this without using strings at all (such as by checking individual characters within the string). Does anyone know how I can manipulate just the number by itself?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
heth123
  • 31
  • 4
  • 2
    Why would you be taking `str` as input to begin with? Are you using `input()` for getting n,m? You can just do `def f(m,n): return n/m` – Akshay Sehgal Jan 20 '21 at 07:04
  • Also why is `650899 / 3 = 4`? what are you trying to achieve? its not clear. – Akshay Sehgal Jan 20 '21 at 07:07
  • Your question is not clear at all. Please modify the question – Prakash Dahal Jan 20 '21 at 07:07
  • firstly 0 in not divisible by any number – Noor Ahmed Natali Jan 20 '21 at 07:07
  • 1
    @Noor, zero is divisible by *every* number, since `n * 0 == 0 for all n`. – paxdiablo Jan 20 '21 at 07:18
  • 4
    Okay, I've rewritten the question to be more explicit as to what was needed. heth123, can you please confirm that this is what you meant? All those that voted to close should think about retracting the vote now, or at least letting me know what other deficiencies exist so we can repair them. I'd say there's a good chance this new version of the question is correct given both myself and Sayandip decoded it to the same question :-) – paxdiablo Jan 20 '21 at 07:23
  • Does this answer your question? [Turn a single number into single digits Python](https://stackoverflow.com/questions/21270320/turn-a-single-number-into-single-digits-python) – Tomerikoo Jan 23 '21 at 00:33
  • Follows by https://stackoverflow.com/questions/8002217/how-do-you-check-whether-a-number-is-divisible-by-another-number-python – Tomerikoo Jan 23 '21 at 00:38

2 Answers2

2

Try the following:

def solution(n: int, m: int) -> int:
    """
    Check how many digits of n, are divisible by m.
    """
    
    ans = 0                    # initiate counter
    while n:
        # generate quotient and remainder when divided by 10
        n, r = divmod(n, 10)
        # increase counter by 1, if remainder is divisible by m
        ans += (r % m) == 0
    return ans

>>> solution(n=650899, m=3)
4
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • Using booleans in arithmetic operations is not so readable. Personally i would prefer an explicit `if`... – Tomerikoo Jan 23 '21 at 00:36
0

Okay, if you're after a count of the digits in your larger number that are an integer multiple of another digit, that can be done with modulo and division:

def digitsThatAreMults(checkNum, digit):
    # Init count to zero, we'll increment for every digit that matches.

    count = 0

    # Continue until no digits left.

    while checkNum != 0:
        # Digit is a multiple if no remainder when dividing.

        if (checkNum % 10) % digit == 0:
            count += 1

        # Debugging line to see in action.

        print("dbg", count, checkNum)

         # Strip off digit you just tested.

        checkNum = checkNum // 10

    # Now just return the count of the matches.

    return count

# Test harness, you may want to add some more test cases for decent coverage :-)

print(digitsThatAreMults(650899, 3))

With the debug line in there, you can see how it works, checking each digit (the one at the end) and increasing the count if it's a multiple:

dbg 1 650899
dbg 2 65089
dbg 2 6508
dbg 3 650
dbg 3 65
dbg 4 6
4

Once you're happy with code, you can delete the debug line, though I tend to leave things in there and just comment them out, since you often have to come back and debug the code for some bizarre edge case you didn't think of :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953