(KevinC's) Triangular DeciDigits Sequence Input:

A positive integer n which is 1 &lt;= n &lt;= 25000.

Output:


In this sequence we start with the decimal number 1/n.
Then we take the sum of digits up until the n'th digit after the comma (1-indexed); followed by the sum of digits up until the (n-1)'th, then (n-2)'th, etc. Continue until n is 1.
The output is the sum of all these combined.


For example:

n = 7
1/7 = 0.1428571428...
7th digit-sum = 1+4+2+8+5+7+1 = 28
6th digit-sum = 1+4+2+8+5+7 = 27
5th digit-sum = 1+4+2+8+5 = 20
4th digit-sum = 1+4+2+8 = 15
3rd digit-sum = 1+4+2 = 7
2nd digit-sum = 1+4 = 5
1st digit     = 1
Output = 28+27+20+15+7+5+1 = 103


Challenge rules:


If the decimal of 1/n doesn't have n digits after the comma, the missing ones will be counted as 0 (i.e. 1/2 = 0.50 =&gt; (5+0) + (5) = 10).
You take the digits without rounding (i.e. the digits of 1/6 are 166666 and not 166667)


General rules:


Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
Default Loopholes are forbidden.
If possible, please add a link with a test for your code.
Also, please add an explanation if necessary.


First 1 - 50 in the sequence:

0, 10, 18, 23, 10, 96, 103, 52, 45, 10, 270, 253, 402, 403, 630, 183, 660, 765, 819, 95, 975, 1034, 1221, 1500, 96, 1479, 1197, 1658, 1953, 1305, 1674, 321, 816, 2490, 2704, 4235, 2022, 3242, 2295, 268, 2944, 3787, 3874, 4097, 1980, 4380, 4968, 3424, 4854, 98


Last 24990 - 25000 in the sequence:

1405098782, 1417995426, 1364392256, 1404501980, 1408005544, 1377273489, 1395684561, 1405849947, 1406216741, 1142066735, 99984
Mathematica, 42 bytes

#&amp;@@RealDigits[1/#,10,#,-1].(#-Range@#+1)&amp;


or

#&amp;@@RealDigits[1/#,10,#,-1].Range[#,1,-1]&amp;


or

Tr@Accumulate@#&amp;@@RealDigits[1/#,10,#,-1]&amp;


Explanation

Take the example from the challenge spec. We want to compute:

  1+4+2+8+5+7+1
+ 1+4+2+8+5+7
+ 1+4+2+8+5
+ 1+4+2+8
+ 1+4+2
+ 1+4
+ 1


Rearranging, this is:

  1*7 + 4*6 + 2*5 + 8*4 + 5*3 + 7*2 + 1*1
= (1, 4, 2, 8, 5, 7, 1) . (7, 6, 5, 4, 3, 2, 1)


where . is the scalar product of two vectors.

That's pretty much all the solution does.

#&amp;@@RealDigits[1/#,10,#,-1]


This gets us the first N decimal digits of 1/N (the #&amp;@@ extracts the first element of the RealDigits result because that also returns the offset of the first digit which we don't care about).

Then we get the list from N down to 1 either using (#-Range@#+1) or Range[#,1,-1], both of which are shorter than Reverse@Range@#, and take the scalar product.

The alternative solution instead uses Accumulate to compute a list of all prefix sums and then adds up those prefix sums with Tr.

Since this is really fast even for large inputs, here is a scatter plot of the sequence up to N = 100,000 (doing all of them and plotting them took a while though):


Click for larger version.

The blue line is the naive upper bound of 9 N (N+1) / 2 (if all the decimal digits were 9) and the orange line is exactly half of that. Unsurprisingly this is right inside the main branch of the plot since, statistically, we'd expect the average digit to be 4.5.

The thin line of plot points which you can see below the main branch are fractions which end in ...3333..., as they all lie very close to 3 N (N+1) / 2.