3 kyu

Find nth Digit In a Infinite Addition Result

127 of 178myjinxin2015

Description:

Problem

Let us write down the infinite consecutive positive integers in a sequence in one line without any space. And then write their squares in the second line. This will generate two different long numbers, now we want to calculate the sum of these two numbers.

Of course, the result of the calculation will be a infinite sequence too. The calculation of the first 30 digits is just as below:

  123456789101112131415161718192...
+ 149162536496481100121144169196...
= 272619325597593231536305887388...

As you can see, the first digit of the result is 2, the second digit is 7, and the third is 2 and so on.

Can you find out the nth digit of this infinite sequence?

Task

You are given an integer n, you should output the digit at position n in the resulting infinite sequence.

Note that n is 0-based.

Still don't understand the problem? Look at the following examples:

Examples

For n = 1, the output should be 7.

272619325597593231536305887388...
 ^
 1st digit

For n = 5, the output should be 9.

272619325597593231536305887388...
     ^
     5th digit

For n = 29, the output should be 8.

272619325597593231536305887388...
                             ^
                             29th digit

Note

  • 0 <= n < 2^31

  • In order to avoid timeout, be aware of the code's performance ;-)

  • Something like require("bignumber.js") was disabled.
  • I'm not sure that my solution is absolutely right (perhaps failed in some edge cases?). If so, please give me some feedback. Thanks ;-)

  • Happy Coding ^_^

Algorithms
Performance

Stats:

CreatedOct 30, 2017
PublishedOct 31, 2017
Warriors Trained2164
Total Skips605
Total Code Submissions4308
Total Times Completed178
JavaScript Completions127
Java Completions57
Total Stars87
% of votes with a positive feedback rating83% of 54
Total "Very Satisfied" Votes42
Total "Somewhat Satisfied" Votes6
Total "Not Satisfied" Votes6
Total Rank Assessments3
Average Assessed Rank
3 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
3 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • kazk Avatar
  • JohanWiltink Avatar
  • Madjosz Avatar
  • ZED.CWT Avatar
  • hobovsky Avatar
Ad