Project Euler task 30

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 is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

================PROGRAM TEXT===============

#include

#define MYTYPE char

__global__ void findNumbers(MYTYPE* _pArray) {
int nThreadNum = blockDim.x * blockIdx.x + threadIdx.x;

int resNum = 0;
int k = nThreadNum;

for (int i = 0; i < 7; i++) {
int num = k % 10;
resNum += num * num * num * num * num;
k /= 10;
}

_pArray[nThreadNum] = (resNum == nThreadNum) ? 1 : 0;
}

int main() {
MYTYPE* d_pArray;
int threadsPerBlock = 128;
int blocksNum = 8192;

size_t nArraySize = threadsPerBlock * blocksNum * sizeof(MYTYPE);

MYTYPE* h_pArray = new MYTYPE[nArraySize];
cudaMalloc(&d_pArray, nArraySize);
cudaMemset(&d_pArray, 0, nArraySize);

// Kernel
findNumbers<<< blocksNum, threadsPerBlock >>>(d_pArray);

cudaMemcpy((void *) h_pArray, d_pArray, nArraySize, cudaMemcpyDeviceToHost);
cudaFree(d_pArray);

int res = 0;
for (int i = 0; i < nArraySize/sizeof(MYTYPE); i++) {
if (i == 1)
continue;

if (h_pArray[i] == 1) {
res += i;
std::cout << i << ": " << (int) h_pArray[i] << std::endl;
}
}

std::cout << res << std::endl;

delete [] h_pArray;

return 0;
}

Comments

Popular Posts