Data representation · A level · OCR H446 1.4.1, AQA 7517 4.5.1.1, Eduqas A500QS 2.3 · about 25 min
Natural, integer, rational, irrational, real and ordinal numbers; any number base; bits, bytes, kilo and kibi.
[1 mark]Which set of numbers is the square root of 2 in, but not in the rational numbers?
[1 mark]Which of these are rational numbers?
Tick every answer that is true.
[1 mark]What are ordinal numbers used for?
[1 mark]Write the denary number 255 in base 8.
[1 mark]How many bytes are in 3 KiB?
[1 mark]What does this program print?
n = 47
digits = ""
while n > 0:
digits = str(n % 5) + digits
n = n // 5
print(digits)142
Repeated division by 5 gives remainders 2, 4, 1, read from last to first: 47 = 1 x 25 + 4 x 5 + 2.
Write to_base(n, base). The parameter n is a whole number, 0 or more; base is a whole number from 2 to 16. It returns the digits of n in that base as a string, using 0 to 9 then A to F, and returns "0" when n is 0. Use repeated division with // and %: you may not use bin, hex, oct or format. The loop at the bottom prints lines such as 173 in base 16 = AD.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
DIGITS = "0123456789ABCDEF"
def to_base(n, base):
# repeated division goes here
return ""
for n, base in [(173, 2), (173, 8), (173, 16), (0, 2), (2024, 16)]:
print(n, "in base", base, "=", to_base(n, base))The hint students can ask for: Divide by the base again and again. Each remainder is one digit, and the first remainder you get is the rightmost digit, so build the string from the right. Decide what happens when n starts at 0.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
DIGITS = "0123456789ABCDEF"
def to_base(n, base):
if n == 0:
return "0"
digits = ""
while n > 0:
digits = DIGITS[n % base] + digits
n = n // base
return digits
for n, base in [(173, 2), (173, 8), (173, 16), (0, 2), (2024, 16)]:
print(n, "in base", base, "=", to_base(n, base))
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.