Back to problems

Fancy Number

Algorithm · Hudson River Trading · Medium

A positive integer is called fancy if its representation in base‑4 contains only the digits 0 and 1. For instance, 17 in base‑4 is 101, so it is fancy; 18 in base‑4 is 102, so it is not. You are given a positive integer n. Implement the function count_fancy(n) that returns the total number of fancy positive integers strictly less than n. Example 1: Explanation: The fancy numbers smaller than 18 are: 1 (base‑4: 1), 4 (base‑4: 10), 5 (base‑4: 11), 16 (base‑4: 100), and 17…

Checking your access…