The following snippet of code doesn't work! Figure out what is wrong and fix the bugs.
""" Compares if a and b are equal.
Hint: If you're stuck, try writing a function that prints out the first 5 perfect squares using a while statement: 1, 4, 9, 16, 25. Then, adapt that while statement to this question by changing the header.
"""Return the largest perfect square less than X, where X>0.
We iterate over perfect squares until we find the first one larger or equal to the input. The answer is then the square before that one. This solution is inefficient, but an efficient solution requires taking a square root.
An open interval is a range of numbers that does not include its end points. For example, (10, 15) stands for all numbers that are strictly greater than 10 and strictly less than 15. Two intervals overlap if they contain any points in common. For example (10, 15) overlaps (14, 16), but not (1, 5) or (15, 16). The intervals (10, 10) or (10, 9) contain no numbers, since nothing is both greater than and less than 10, or greater than 10 and less than 9. Implement the function overlaps to take four numbers as arguments, representing the bounds of two intervals, and return True if the intervals overlap and False otherwise.
There are many solutions to this problem. One way to look at it is to consider conditions under which the intervals don't overlap. Clearly for two non-empty not to overlap, one has to come entirely before the other. This becomes high1 <= low0 or high0 <= low1, which when negated is high1 > low0 and high1 > low1. In addition, both lower bounds must be less than their respective upper bounds (or the intervals are empty). The solution given combines these observations.
The nth triangular number is defined as the sum of all integers from 1 to n, i.e.
Define triangular_sum, which takes an integer n and returns the sum of the first n triangular numbers, while printing each of the triangular numbers between 1 and the nth triangular number.
"""Return whether b is in hailstone sequence of a."""
Implement the function nearest_two, which takes as input a positive number x and returns the power of two (..., 1/8, 1/4, 1/2, 1, 2, 4, 8, ...) that is nearest to x. If x is exactly between two powers of two, return the larger.
You may change the starter implementation if you wish.
"""Return the power of two that is nearest to x.
This implementation repeatedly doubles or halves the number power_of_two until reaching the closest number to x. The last three lines enforce the tie-breaking policy when x is exactly betweeen two powers of two.
Complete the implementation of pi_fraction, which takes a positive number gap and prints the fraction that is no more than gap away from pi and has the smallest possible positive integer denominator. See the doctests for the format of the printed output.
Hint: If you want to find the nearest integer to a number, use the built-in round function. It's possible to solve this problem without using round.
"""Print the fraction within gap of pi that has the smallest denominator.
This implementation repeatedly increases denominator until the nearest fraction to pi is within gap.