https://github.com/mtlh/digits0ton
https://edabit.com/challenge/j9zed4GnykS48W6vh My solution to this problem in Python and JavaScript.
https://github.com/mtlh/digits0ton
algorithm coding-challenge javascript python
Last synced: 4 months ago
JSON representation
https://edabit.com/challenge/j9zed4GnykS48W6vh My solution to this problem in Python and JavaScript.
- Host: GitHub
- URL: https://github.com/mtlh/digits0ton
- Owner: mtlh
- Created: 2020-08-24T09:27:28.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-24T09:30:40.000Z (over 5 years ago)
- Last Synced: 2025-01-03T04:47:06.744Z (11 months ago)
- Topics: algorithm, coding-challenge, javascript, python
- Homepage: https://edabit.com/challenge/j9zed4GnykS48W6vh
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# How Many Digits between 1 and N
https://edabit.com/challenge/j9zed4GnykS48W6vh My solution to this problem in Python and JavaScript.
## Python
def digits(number):
number-=1
n = 10**(len(str(number))-1)
digits = 0
while n>=1:
digits+=(number-n+1)*len(str(number))
number=n-1
n//=10
return digits
## JavaScript
function digits(number) {
if (typeof number === 'bigint') {
return bigIntDigits(number)
}
let amountOfDigits = 0
for (let i = 1; i <= number; i *= 10) {
amountOfDigits += number - i
}
return amountOfDigits
}
function bigIntDigits(number) {
const bigNumber = BigInt(number)
let amountOfDigits = 0n
for (let i = 1n; i <= bigNumber; i *= 10n) {
amountOfDigits += bigNumber - i
}
return amountOfDigits
}