-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PROBLEM 310: | ||
|
||
Write an algorithm that finds the total number of set bits in all integers between 1 and N. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
Problem: | ||
Write an algorithm that finds the total number of set bits in all integers between 1 | ||
and N. | ||
""" | ||
|
||
|
||
def get_set_bits(num: int) -> int: | ||
# get the number of bits set in a number [runs in O(log(n))] | ||
bin_num = bin(num)[2:] | ||
return sum([int(digit) for digit in bin_num]) | ||
|
||
|
||
def get_total_set_bits(N: int) -> int: | ||
# sums up the number of bits set in all positive numbers till N | ||
result = 0 | ||
for i in range(1, N + 1): | ||
result += get_set_bits(i) | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
print(get_total_set_bits(0)) | ||
print(get_total_set_bits(1)) | ||
print(get_total_set_bits(2)) | ||
print(get_total_set_bits(3)) | ||
print(get_total_set_bits(4)) | ||
print(get_total_set_bits(5)) | ||
|
||
|
||
""" | ||
SPECS: | ||
TIME COMPLEXITY: O(n x log(n)) | ||
SPACE COMPLEXITY: O(1) | ||
""" |