This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
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.
Moved common functions from solutions 4 and 16 into lib/digits.py, which has a function that returns a list containing the digits of an input number.
- Loading branch information
Showing
4 changed files
with
35 additions
and
8 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
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,8 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import sys | ||
sys.path.append('../lib') | ||
from digits import get_digits | ||
|
||
print sum(reversed(get_digits(2 ** 1000))) |
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,6 @@ | ||
# Problem 16 | ||
|
||
2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. | ||
|
||
What is the sum of the digits of the number 2^(1000)? | ||
|
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,14 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
def get_digits(number): | ||
""" Return the digits present in the number """ | ||
digits = [] | ||
divisor = 10 | ||
while number * 10.0 / divisor > 0.1: | ||
remainder = number % divisor | ||
digits.append(int(remainder / (divisor / 10))) # Find the first digit of this number | ||
number = number - remainder # Round-up the number so that there is no extra remainder | ||
divisor = divisor * 10 # increase the divisor to get the next number | ||
|
||
return digits |