Project Euler:Problem 29 Distinct powers

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

22=4, 23=8, 24=16, 25=32

32=9, 33=27, 34=81, 35=243

42=16, 43=64, 44=256, 45=1024

52=25, 53=125, 54=625, 55=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

#include <iostream>
#include <map>
#include <string>
using namespace std;

void reverse_data(string &data)
{
	char temp = '0';
	int start = 0;
	int end = data.size() - 1;

	while (start < end)
	{
		temp = data[start];
		data[start++] = data[end];
		data[end--] = temp;
	}
}
void compute_value(string lhs, string rhs, string &result)
{
	reverse_data(lhs);
	reverse_data(rhs);
	int i = 0, j = 0, res_i = 0;
	int tmp_i = 0;
	int carry = 0;

	for (i = 0; i != lhs.size(); ++i, ++tmp_i)
	{
		res_i = tmp_i;  //在每次计算时,结果存储的位需要增加
		for (j = 0; j != rhs.size(); ++j)
		{
			carry += (result[res_i] - '0') + (lhs[i] - '0') * (rhs[j] - '0');//此处注意,每次计算并不能保证以前计算结果的进位都消除, 并且以前的计算结果也需考虑。
			result[res_i++] = (carry % 10 + '0');
			carry /= 10;
		}
		while (carry)//乘数的一次计算完成,可能存在有的进位没有处理
		{
			result[res_i++] = (carry % 10 + '0');
			carry /= 10;
		}
	}
	for (int i = result.size() - 1; i >= 0; i--)
	{
		if (result[i] != '0')
			break;
		else
			result.pop_back();
	}
	reverse_data(result);
}

string powe(string a, int b)
{
	string res = a;
	for (int ii = 1; ii < b; ii++)
	{
		string tmp(res.length()+a.length(),'0');
		compute_value(res, a, tmp);
		res = tmp;
	}
	return res;
}

string int_str(int a)
{
	string s = "";
	while (a != 0)
	{
		int tp = a % 10;
		char p = tp + '0';
		s = p + s;
		a = a / 10;
	}
	return s;
}

int main()
{
	map<string, int>mp;
	for (int a = 2; a <= 100; a++)
	{
		string s = int_str(a);
		for (int b = 2; b <= 100; b++)
		{
			string res = powe(s, b);
			mp[res] = 1;
		}
	}

	map<string, int>::iterator iter;
	int count = 0;
	for (iter = mp.begin(); iter != mp.end(); iter++)
	{
		if (mp[iter->first] == 1)
			count++;
	}
	cout << count << endl;
	system("pause");
	return 0;
}
时间: 2024-10-14 16:38:10

Project Euler:Problem 29 Distinct powers的相关文章

Project Euler:Problem 47 Distinct primes factors

The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The first three consecutive numbers to have three distinct prime factors are: 644 = 22 × 7 × 23 645 = 3 × 5 × 43 646 = 2 × 17 × 19. Find the first four co

Project Euler:Problem 48 Self powers

The series, 11 + 22 + 33 + ... + 1010 = 10405071317. Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. #include <iostream> #include <string> using namespace std; string powe(int n) { string s = "1"; for (int i =

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;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 v

Project Euler:Problem 30 Digit fifth powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: 1634 = 14 + 64 + 34 + 44 8208 = 84 + 24 + 04 + 84 9474 = 94 + 44 + 74 + 44 As 1 = 14 is not a sum it is not included. The sum of these numbers

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

Project Euler:Problem 58 Spiral primes

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18  5  4  3 12 29 40 19  6  1  2 11 28 41 20  7  8  9 10 27 42 21 22 23 24 25 26 43 44 45 46