Project Euler #40: Champernowne’s constant

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 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
#include <iostream>
#include <vector>

void champernowneConcat(int foo, std::vector<int> &bar);
int main(void)
{
	std::vector<int> decBuff;
	// Set to 1 to multiply against
	unsigned long long total = 1;


	// Start building up the Champernowne constant
	for(int i = 1; decBuff.size() <= 1000000; i++)
	{
		champernowneConcat(i, decBuff);
	}

	// Calculate the total
	total *= (decBuff[0] * decBuff[9] * decBuff[99] * decBuff[999] * decBuff[9999] * decBuff[99999] * decBuff[999999]); 


	std::cout << "The answer is " << total;

	return 0;
}


void champernowneConcat(int foo, std::vector<int> &bar)
{
	std::vector<int> vecBuff; // separate vector to append from, as opposed to incremented iterator to back 

	while(foo) // while the number is not 0
	{
		vecBuff.insert(vecBuff.begin(), (foo % 10)); // pull the ones spot out
		foo /= 10; // divide equal 10 to effectively shift number right
	}

	for(auto it : vecBuff) // iterate through and append, effective with how short the calculations are
	{
		bar.push_back(it);
	}

	return;
}

Leave a Reply

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