Bit1046 leftmost Digit

Source: Internet
Author: User
Leftmost digit time limit: 1 second memory limit: 1024kb

Description

Given a positive integer N, please write a program to get the leftmost digit of N !. (1 <= n <= 10 ^ 7 ).

Input

The input contains several test cases, the first line of the input is an integer T (t <= 1000), indicates the number of test cases.

The next n lines each has an positive N (1 <= n <= 10 ^ 7 ).

Output

For each test case, please write a line which is the leftmost digit of N !.

Sample Input

3

1

5

10

Sample output

1

1

3

Thanks to the idea provided by the great gods: converting a factorial into a logarithm addition operation solves the problem of high precision. And the sterling Formula

.

Reference: zhangwei1120112119 column bit1046 leftmost Digit

 
# Include <stdio. h> # include <math. h> # define PI ACOs (-1.0) # define e exp (1.0) int main () {int testcases; int N; double result_log; int I; scanf ("% d", & testcases); While (testcases --) {scanf ("% d", & N); result_log = 0; If (n> 500) {result_log = log10 (2 * n * PI)/2.0; result_log + = N * (log10 (N)-log10 (e);} else {++ N; for (I = 2; I <n; ++ I) {result_log + = log10 (I) ;}} result_log-= floor (result_log ); printf ("% d \ n", INT (POW (10.0, result_log);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.