Write a function that takes a positive integer as input and returns the leading digit in its decimal representation. For example, the leading digit of 123456 is 1. Can you do this without any conversions/casts to a string?
Write a function that takes a positive integer as input and returns the leading digit in its decimal representation. For example, the leading digit of 123456 is 1. Can you do this without any conversions/casts to a string?
Permalink: http://problemotd.com/problem/leading-digit/
Content curated by @MaxBurstein
Comments:
Akshay Bist - 9 years, 8 months ago
Assuming only positive integers will be passed
reply permalink
Tibor - 9 years, 8 months ago
Using logarithms instead of loops (Pascal)
The floating point operations might eat your hamster below.
reply permalink
Anonymous - 9 years, 8 months ago
reply permalink
k4rtik - 9 years, 8 months ago
A tiny Python function, assuming correct inputs, almost too easy:
reply permalink