#include <iostream>
#include <stdio.h>
int digit[20] = {};
bool IsRoot(long long polynomial, long long root)
{
if (root == 0)
{
return polynomial % 10 == 0;
}
int curr_pos = 0;
long long n = polynomial;
do
{
digit[curr_pos] = n % 10;
curr_pos++;
n /= 10;
}
while (n);
long long value = 0;
for (int i = curr_pos-1; i >= 0; i--)
{
value *= root;
value += digit[i];
}
return value == 0;
}
bool HasIntegerRoot(long long polynomial)
{
for (int i = 0; i > -10; i--)
{
if (IsRoot(polynomial, i))
{
return true;
}
}
return false;
}
// Z(1) 0
// Z(10) 1
// Z(100) 33
// Z(1000) 172
// Z(10000) 1754
// Z(100000) 14696
// Z(1000000) 152960
long long Z(long long k)
{
long long count = 0;
for (long long n = 1; n <= k; n++)
{
count += HasIntegerRoot(n);
}
return count;
}
int main(int argc, char* argv[])
{
std::cout << Z(1) << std::endl;
std::cout << Z(10) << std::endl;
std::cout << Z(100) << std::endl;
std::cout << Z(1000) << std::endl;
std::cout << Z(10000) << std::endl;
std::cout << Z(100000) << std::endl;
std::cout << Z(1000000) << std::endl;
return 0;
}
2013年8月12日 星期一
Project Euler 269 -- Brute force for small n
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言