Project Euler

Project Euler Problem 29

steloflute 2012. 6. 9. 00:01

Problem 29

25 October 2002

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?


Answer:
9183

 

 

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) {            
            var s = new HashSet<BigInteger>();
            for (int a = 2; a <= 100; a++) {
                for (int b = 2; b <= 100; b++) {
                    s.Add(BigInteger.Pow(a, b));
                }
            }
            Console.WriteLine(s.Count);
        }
    }
}

 

Racket

 

#lang racket
(define (problem29)
  (define s (set))
  (for* ([a (in-range 2 101)]
         [b (in-range 2 101)])
    (set! s (set-add s (expt a b))))
  (displayln (set-count s)))
(problem29)
 


 



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

Project Euler Problem 31  (0) 2012.06.09
Project Euler Problem 30  (0) 2012.06.09
Project Euler Problem 28  (0) 2012.06.09
Project Euler Problem 27  (0) 2012.06.08
Project Euler Problem 26  (0) 2012.06.08