Project Euler

Project Euler Problem 30

steloflute 2012. 6. 9. 00:02

Problem 30

08 November 2002

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.


Answer:
443839

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(Enumerable.Range(10, 500000)
                .Where(n => n.ToString().Select(x => x - '0')
                .Select(x => x * x * x * x * x).Sum() == n)
                .Sum());
        }
    }
}

 

Racket

 

#lang racket
(define (number->digits x)
  (map (lambda (x) (- (char->integer x)
                      (char->integer #\0))) (string->list (number->string x))))

(define (problem30)
  (displayln
   (apply +
          (filter
           (lambda (x) (= (apply +
                                 (map
                                  (lambda (x) (* x x x x x)) (number->digits x)))
                          x)) (range 10 500001)))))
(problem30)

 

'Project Euler' 카테고리의 다른 글

Project Euler Problem 32  (0) 2012.06.09
Project Euler Problem 31  (0) 2012.06.09
Project Euler Problem 29  (0) 2012.06.09
Project Euler Problem 28  (0) 2012.06.09
Project Euler Problem 27  (0) 2012.06.08