Work 11: 10/3

posted Oct 3, 2018, 12:32 PM by JonAlf Dyrland-Weaver
Write the following functions in racket

  • sumDigs
    • Takes 1 parameter representing a nonnegative integer
    • Returns the sum of all digits in the number
    • Use a recursive method to solve this problem
    • Examples:
      • (sumDigs 0) ==> 0
      • (sumDigs 5) ==> 5
      • (sumDigs 612) ==> 9
  • sumRange
    • Takes 2 integer arguments
    • Returns the sum of all the integers between the 2 arguments
    • Examples
      • (sumRange 4 4) ==> 4
      • (sumRange 3 8) ==> 33
      • (sumRange 8 3) ==> 33
Challenge time!
You do not need to complete these problems. Please do not select working on these over bodily necessities, like sleep.
  • The numbers we most commonly interact with are written in a decimal, or base 10, system.
    • This means that every digit represents a multiplier of a power of 10. 
      • For example, 45610 = 4*102 + 5 * 101 + 6 * 100.
    • We use this fact to help solve problems involving the individual digits of a number.
  • There are other base systems
    • In binary (base 2), we use powers of 2 instead of powers of 10
      • For example, 10112 = 1 * 23 + 0 * 22 + 1 * 21 + 1 *20
    • In octal (base 8), we use powers of 8.
      • 138 = 1 * 81 + 3 * 80
  • Write a function that takes a positive decimal integer and returns the number of digits that would be needed to represent that number in binary.
    • (binaryDigits 11) ==> 4
    • (binaryDigits 4) ==> 3
  • Write a function that takes 2 positive decimal integers as arguments and returns the number of digits that would be needed to represent the first argument in the seconds base.
    • (baseNDigits 457 8) would return the numbers of digits needed to represent 45710 in octal.

submit this under recursion
Comments