1. Home
  2. Explore
  3. Computer Science
  4. Modular Arithmetic in Python

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Modular Arithmetic in Python

  • 44Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Modulo Foundations
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What does the modulo operation calculate?

Tap to flipClick to flipspace

The remainder left over after dividing one number by another.

Or swipe the card

44 cards

  1. What does the modulo operation calculate?

    The remainder left over after dividing one number by another.

  2. In a mod ba \bmod bamodb, what does aaa represent?

    aaa is the dividend: the number being divided.

  3. In a mod ba \bmod bamodb, what does bbb represent?

    bbb is the divisor and modulus.

  4. What does the quotient represent in a division used to find a remainder?

    The number of times the divisor goes into the dividend.

  5. What does the modulus determine about possible outputs?

    It sets the range of possible remainders.

  6. What is 6 mod 26 \bmod 26mod2, and why?

    000, because 222 goes into 666 three times with nothing left over.

  7. What is 5 mod 25 \bmod 25mod2, and why?

    111, because 222 goes into 555 twice, leaving a remainder of 111.

  8. How can you find the remainder after dividing aaa by bbb using the quotient qqq?

    Subtract the product of the quotient and divisor from the dividend: a−qba - qba−qb.

  9. Which Python operator performs a modulo operation?

    The percent sign, %.

  10. Is Python's % modulo operator limited to integers?

    No. It works with integers and floating-point numbers, though the examples here focus on integers.

  11. What is fmod used for in Python?

    It is an alternative function for performing modulo operations.

  12. What should you consult to understand differences between Python's modulo approaches, such as % and fmod?

    Documentation or other references that explain how the functions differ.

  13. How can Python's modulo result be checked against a hand calculation?

    Compute the remainder manually, then evaluate the same operands with % and compare the results.

  14. What does floor division return in Python?

    The whole-number quotient: how many times the divisor goes into the dividend.

  15. How can floor division be used to calculate a remainder from dividend aaa and divisor bbb?

    Use a−(a//b)×ba - (a // b) \times ba−(a//b)×b.

  16. What is the result of 5−(5//2)×25 - (5 // 2) \times 25−(5//2)×2?

    111: floor division gives a quotient of 222, and 5−2×2=15 - 2 \times 2 = 15−2×2=1.

  17. What are the two inputs to the custom modulo function described in the content?

    The dividend and the modulus (divisor).

  18. What does a custom modulo function return after applying the floor-division formula?

    The remainder, calculated by subtracting the divisor-times-quotient product from the dividend.

  19. Which two examples were used to check the custom modulo function?

    6 mod 2=06 \bmod 2 = 06mod2=0 and 5 mod 2=15 \bmod 2 = 15mod2=1.

  20. How can a custom modulo function be tested over many integer inputs?

    Use nested loops over a range of dividends and moduli, and compare the custom result with Python's % result.

  21. In the described loop test, what does a mismatch between the custom result and % indicate?

    It indicates a possible error in the custom modulo function.

  22. What is 9 mod 99 \bmod 99mod9?

    000, because 999 goes into 999 once with no remainder.

  23. What is 9 mod 89 \bmod 89mod8?

    111, because 888 goes into 999 once, leaving 111.

  24. Why use modulo arithmetic when advancing an hour on a 24-hour clock?

    It wraps the hour back to the start of the day after it reaches 242424.

  25. What modulus is used to wrap hours on a 24-hour clock?

    242424.

  26. How is an updated clock hour calculated from the current hour and an offset?

    Add the offset to the current hour, then take the result modulo 242424.

  27. How does the clock function handle an offset of zero?

    It displays the current hour, because no hours are added or subtracted.

  28. How can the clock function calculate a future time?

    Pass a positive number of hours as the offset.

  29. How can the clock function calculate a past time?

    Pass a negative number of hours as the offset.

  30. If the current hour is 232323, what hour results from adding one hour modulo 242424?

    000; the clock wraps around to the beginning of the day.

  31. When the clock function changes the hour, what happens to the minutes?

    The minutes remain the current minutes; the function changes only the hour.

  32. How is 9 p.m. written in the military-time style described?

    210021002100.

  33. Why does the clock function add a leading zero to a one-digit hour or minute?

    To keep the military-time display in a two-digit hour and minute format, such as 03 or 09.

  34. Does the described clock function print its formatted time or return it?

    It prints the formatted time rather than returning a value.

  35. Why does the clock code use a time-zone-aware current time?

    Without a specified time zone, the time may be returned in universal time rather than the desired local zone.

  36. What Python package is used in the example to work with time zones?

    pytz.

  37. What is the example clock function's default time zone?

    Eastern Standard Time.

  38. How can the clock function display a time in a different time zone?

    Supply that location's time-zone name as the time-zone argument.

  39. How can you find available time-zone names using pytz?

    Use its all_timezones list.

  40. How is modular arithmetic used in the Diffie–Hellman key exchange?

    It is used as part of a cryptographic method that lets two parties securely exchange keys over a public channel.

  41. How can modular arithmetic help grocery stores track inventory?

    Checksums associated with product barcodes can use modular arithmetic to help verify UPC codes.

  42. What is the role of a checksum in a UPC code?

    It uses modular arithmetic to help verify that the UPC is valid.

  43. How many digits does the content describe as comprising a UPC?

    Ten digits.

  44. How is modular arithmetic used with ISBNs?

    It is used in processes that create or verify the unique values associated with books.

Card 1 of 44
Card 1 of 44

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent