Project Euler – Problem 40 Solution

Problem

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021…

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 x d10 x d100 x d1000 x d10000 x d100000 x d1000000

Solution

A pretty straight forward solution, the only trick is to turn the sequence of natural numbers 1, 2, 3, … 11, 12, 13.. into a continuous sequence of individual numbers. I did so by taking each number and turning it into a string array of its digits (e.g. 11 –> {"1"; "1"}) and concatenating these arrays using Seq.collect.

 

Learn to build Production-Ready Serverless applications

Want to learn how to build Serverless applications and follow best practices? Subscribe to my newsletter and join over 5,000 AWS & Serverless enthusiasts who have signed up already.

1 thought on “Project Euler – Problem 40 Solution”

  1. Pingback: Project Euler 40: Find the nth digit of an irrational number | mathblog.dk

Leave a Comment

Your email address will not be published. Required fields are marked *