// NARCISSISTIC NUMBER FINDER
// By Eliot Ball - British Informatics Olympiad 2011 Finalist
// contact@eliotball.com

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

bool checknarc(int n)
{
	// Find the number of digits in the number by taking log to the base 10 and rounding down, add one
	double digits = floor(log10(double(n))) + 1;

	// Prepare the variables for keeping track of the powers of the digits
	int sumpow = 0;
	int n2 = n;

	// Remove the digits one at a time, raising them to the power of the number of digits and adding
	while (n2 != 0)
	{
		sumpow += pow(double(n2 % 10), digits);
		n2 /= 10;
	}

	// If the result is the same as the original number, this is a narcissistic number
	if (sumpow == n)
	{
		// Wooo! Resssult!
		return true;
	}
	return false;
}

int main()
{
	// Ask for the number of digits in the narcissistic numbers we will search for
	int digits;
	cout << "How many digits?" << endl;
	cin >> digits;

	// Iterate through all of the numbers with that number of digits
	for (int i = floor(pow(10.0, double(digits - 1))); i < floor(pow(10.0, double(digits))); i++)
	{
		// Check if this is a narcissistic number
		if (checknarc(i))
		{
			// Yaaay, we found one, output it to the screen!
			cout << i << endl;
		}
	}

	// Say that we are done
	cout << "Done" << endl;

	// Stop the program from quitting with an infinite loop
	while (true) { }
	return 0;
}
