Shoutout To Number Theory Enthusiasts!

Find the 40th digit from the left of the expansion of 123 ! 123! .


Details and Assumptions:

  • ! ! denotes the factorial operator. (For example, 7 ! = 7 × 6 × 5 × . . . × 2 × 1 7! = 7 \times 6 \times 5 \times ... \times 2 \times 1 )
  • As an example, the 9th digit from the left of 12342433 2 432 is 2.
4 7 6 2 1 3 9 5

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Fletcher Mattox
May 1, 2021
1
2
 from math import factorial
 print(str(factorial(123))[39])

1
5

A purely mathematical solution would've worked:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import math

#Defining the factorial function using recursion
def fact(n):
    if n == 0:
        return 1
    else:
        return n * fact(n-1)

n=fact(123)
j=0
k=40
temp=n
while(temp >= 1):
    j += 1
    temp /= 10
temp=n
temp = math.floor(temp/10**(j-k))
print(temp % 10)

But it doesn't show proper results for any digit past the 1 5 t h 15^{th} digit of a number consisting of more than or equal to 16 16 digits.

So, we convert the number to a string datatype and print the character from the required position:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#Defining the factorial function using recursion
def fact(n):
    if n == 0:
        return 1
    else:
        return n * fact(n-1)

n=fact(123)
k=40
#Converting the number into a string
n=str(n)

#n[k] refers to the character in the kth index of the array of characters of the string n filled up in the same order as in the string
#array indices start from 0,1,2,3,... which represent positions 1,2,3,4,... respectively
#So the character at position k must be at index (k-1), so the character at the (k-1)th index of the character array n is the
#digit at position k

print(n[k-1])


Alternative C++ equivalent: (Although this too can't process very large values and is evidently not gonna work here.)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<cmath>
using namespace std;

main()
{
    int n,k,j,temp,rem;
    cout<<"Enter a number : ";
    cin>>n;
    input:
    cout<<"Enter the position of the digit : ";
    cin>>k;
    temp=n;
    j=0;
    while(temp >= 1)
    {
        j++;
        temp /= 10;
    }
    if(k > j)
    {
        cout<<"\nThe entered number has only "<<j<<" digits. Try again.\n";
        goto input;
    }
    temp=n;
    temp /= pow(10,j-k);
    temp=floor(temp);
    rem=temp%10;
    cout<<"The digit at position "<<k<<" of "<<n<<" is "<<rem;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...