Comparing the O[n] time solution with the "constant time" O[1] solution provided in other answers goes to show that if the O[n] algorithm is fast enough, n may have to get very large before it is slower than a slow O[1].
The strings version is approx. 60% faster than the "maths" version for numbers of 20 or fewer digits. They become closer only when then number of digits approaches 200 digits
# the "maths" version
import math
def first_n_digits1[num, n]:
return num // 10 ** [int[math.log[num, 10]] - n + 1]
%timeit first_n_digits1[34523452452, 2]
1.21 µs ± 75 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit first_n_digits1[34523452452, 8]
1.24 µs ± 47.5 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
# 22 digits
%timeit first_n_digits1[3423234239472523452452, 2]
1.33 µs ± 59.4 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit first_n_digits1[3423234239472523452452, 15]
1.23 µs ± 61.2 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
# 196 digits
%timeit first_n_digits1[3423234239472523409283475908723908723409872390871243908172340987123409871234012089172340987734507612340981344509873123401234670350981234098123140987314509812734091823509871345109871234098172340987125988123452452, 39]
1.86 µs ± 21.8 ns per loop [mean ± std. dev. of 7 runs, 100000 loops each]
# The "string" verions
def first_n_digits2[num, n]:
return int[str[num][:n]]
%timeit first_n_digits2[34523452452, 2]
744 ns ± 28.1 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit first_n_digits2[34523452452, 8]
768 ns ± 42.7 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
# 22 digits
%timeit first_n_digits2[3423234239472523452452, 2]
767 ns ± 33.6 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit first_n_digits2[3423234239472523452452, 15]
830 ns ± 55.1 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
# 196 digits
%timeit first_n_digits2[3423234239472523409283475908723908723409872390871243908098712340987123401208917234098773450761234098134450987312340123467035098123409812314098734091823509871345109871234098172340987125988123452452, 39]
1.87 µs ± 140 ns per loop [mean ± std. dev. of 7 runs, 100000 loops each]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a number and to find first and last digit of a number.
Examples:
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 2
To find last digit of a
number, we use modulo operator %. When modulo divided by 10 returns its last digit.
Suppose if n = 1234
then last Digit = n % 10 => 4
To finding first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.
Approach 1 [With loop]:
C++
#include
using
namespace
std;
int
firstDigit[
int
n]
{
while
[n >= 10]
n /= 10;
return
n;
}
int
lastDigit[
int
n]
{
return
[n % 10];
}
int
main[]
{
int
n = 98562;
cout
Javascript
function
firstDigit[n]
{
let digits = Math.floor[Math.log[n]/Math.log[10]]
n = Math.floor[n / Math.pow[10, digits]]
return
n;
}
function
lastDigit[n]{
return
[n % 10]
}
let n = 98562;
document.write[firstDigit[n],
" "
]
document.write[lastDigit[n],
""
]
Time Complexity: O[1]
Auxiliary Space: O[1]
Important note: log10[] is a mathematical function present in math.h header file. It returns log base 10 value of the passed parameter to log10[] function.