home > Project Euler >

ForNext

Shut the fuck up and write some code

Problem 23

2つの過剰数の和で書き表せない正の整数の総和を求めよ

完全数とは, その数の真の約数の和がそれ自身と一致する数のことである. たとえば, 28の真の約数の和は, であるので, 28は完全数である.

真の約数の和がその数よりも少ないものを不足数といい, 真の約数の和がその数よりも大きいものを過剰数と呼ぶ.

12は, となるので, 最小の過剰数である. よって2つの過剰数の和で書ける最少の数は24である. 数学的な解析により, 28123より大きい任意の整数は2つの過剰数の和で書けることが知られている. 2つの過剰数の和で表せない最大の数がこの上限よりも小さいことは分かっているのだが, この上限を減らすことが出来ていない.

2つの過剰数の和で書き表せない正の整数の総和を求めよ.

Non-abundant sums

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be , which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, , the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

考え方

10以下の数の総和は 55

1, 4, 7のうちの、2つの数の和で表すことができる数は、







そのうち、10以下の数の和は

2つの数の和で書き表せない数の和は、


2つの数の和で表すことができるか、1個づつ確認するよりは、こっちの方が速い

VBScript

JScript

Perl

PHP

Python

Ruby

PowerShell

Scala

更新日 : 2013.02.09

与えられた数を素因数分解して、素因数とその指数とを Map にして返す関数

scala> def get_prime_factor(map:collection.mutable.Map[Long, Long], n: Long, factor: Long = 2) {
     |     if  (n  >= factor) {
     |         if (n % factor != 0 )
     |             get_prime_factor(map, n, factor + 1)
     |         else {
     |             if (map.contains(factor))
     |                 map(factor) += 1
     |             else
     |                 map += factor -> 1
     |
     |             get_prime_factor(map, n / factor, factor)
     |         }
     |     }
     | }
get_prime_factor: (map: scala.collection.mutable.Map[Long,Long],n: Long,factor: Long)Unit

真の約数の和を返す関数

scala> def sum_of_proper_divisors(n:Long):Long = {
     |     var map = collection.mutable.Map[Long, Long] ()
     |     get_prime_factor(map, n)
     |     if (map.size == 0) 0
     |     else {
     |         map.transform((k,v) => (math.pow(k, v + 1) - 1).toLong / (k - 1))
     |         map.values.reduceLeft(_*_) - n
     |     }
     | }
sum_of_proper_divisors: (n: Long)Long

過剰数の List を 作成

scala> val abundant_list:List[Long] = (12L to 28123L).toList.filter(n => sum_of_proper_divisors(n) > n)
abundant_list: List[Long] = List(12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102, 104, 108, 112, 114, 120, 126, 132, 138, 140, 144, 150, 156, 160, 162, 168, 174, 176, 180, 186, 192, 196, 198, 200, 204, 208, 210, 216, 220, 222, 224, 228, 234, 240, 246, 252, 258, 260, 264, 270, 272, 276, 280, 282, 288, 294, 300, 304, 306, 308, 312, 318, 320, 324, 330, 336, 340, 342, 348, 350, 352, 354, 360, 364, 366, 368, 372, 378, 380, 384, 390, 392, 396, 400, 402, 408, 414, 416, 420, 426, 432, 438, 440, 444, 448, 450, 456, 460, 462, 464, 468, 474, 476, 480, 486, 490, 492, 498, 500, 504, 510, 516, 520, 522, 528, 532, 534, 540, 544, 546, 550, 552, 558, 560, 564, 570, 572, 576, 580, 582, 588, 594, 600, 606, 608, 612, 616, 618, 620, 624, 630, 636, 640, 642, 644, ...
scala> abundant_list.length
res0: Int = 6965

2つの過剰数の和で書ける数の Set を 作成

scala> val abundant_set = Set (
     |     for (x <- abundant_list) yield {
     |         for (y <- (abundant_list.filter(n => n >= x))) yield {
     |             val i = x + y
     |             if (i <= 28123L) i else 0
     |         }
     |     }
     | ).flatten
java.lang.OutOfMemoryError: Java heap space
        at scala.collection.mutable.ListBuffer.$plus$eq(ListBuffer.scala:119)
        at scala.collection.mutable.ListBuffer.$plus$eq(ListBuffer.scala:42)
        at scala.collection.TraversableLike$$anonfun$filter$1.apply(TraversableLike.scala:240)
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:61)
        at scala.collection.immutable.List.foreach(List.scala:45)
        at scala.collection.TraversableLike$class.filter(TraversableLike.scala:239)
        at scala.collection.immutable.List.filter(List.scala:45)
        at $anonfun$1.apply(<console>:10)
        at $anonfun$1.apply(<console>:9)
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:206)
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:206)
        at scal...

こうなると、ちょっと手に負えない...

scala> var abundant_set:Set[Long] = Set ()
abundant_set: Set[Long] = Set()

scala> for (x <- abundant_list) {
     |     for (y <- (abundant_list.filter(n => n >= x))) {
     |         val i = x + y
     |         if (i <= 28123L) abundant_set = abundant_set + i
     |     }
     | }

2つの過剰数の和で書けない数の総和

scala> abundant_set.sum
res3: Long = 391285755

scala> (1L to 28123L).toList.sum - abundant_set.sum
res4: Long = 4179871

F#

更新日 : 2013.02.06

与えられた数を素因数分解して、素因数とその指数とを Map にして返す関数

> let rec get_prime_factor (map:Map<int, int> byref) (n:int) (factor: int) =
-     if n  >= factor then
-         if n % factor <> 0 then
-             get_prime_factor &map n (factor + 1)
-         else
-             if Map.containsKey factor map then
-                 let n = map.[factor] + 1
-                 map <- map.Remove factor
-                 map <- (map |> Map.add factor n)
-             else
-                 map <- (map |> Map.add factor 1)
-
-             get_prime_factor &map (n / factor) factor
- ;;

val get_prime_factor : byref<Map<int,int>> -> int -> int -> unit

真の約数の和を返す関数

> let sum_of_proper_divisors (n:int):int =
-     let mutable map:Map<int, int> = Map.empty
-     get_prime_factor &map n 2
-
-     if map.IsEmpty then 0
-     else
-         int (map
-         |> Map.map (fun k t -> (System.Math.Pow(float k,(float t) + 1.0) - 1.0) / float (k - 1))
-         |> Map.toList
-         |> List.map (fun (k, t) -> t)
-         |> List.reduce(*)) - n
- ;;

val sum_of_proper_divisors : int -> int

過剰数の List を 作成

> let abundant_list = (
-     [12..28123]
-     |> List.filter(fun n -> (sum_of_proper_divisors n) > n)
- )
- ;;

val abundant_list : int list =
  [12; 18; 20; 24; 30; 36; 40; 42; 48; 54; 56; 60; 66; 70; 72; 78; 80; 84; 88;
   90; 96; 100; 102; 104; 108; 112; 114; 120; 126; 132; 138; 140; 144; 150;
   156; 160; 162; 168; 174; 176; 180; 186; 192; 196; 198; 200; 204; 208; 210;
   216; 220; 222; 224; 228; 234; 240; 246; 252; 258; 260; 264; 270; 272; 276;
   280; 282; 288; 294; 300; 304; 306; 308; 312; 318; 320; 324; 330; 336; 340;
   342; 348; 350; 352; 354; 360; 364; 366; 368; 372; 378; 380; 384; 390; 392;
   396; 400; 402; 408; 414; 416; ...]

> abundant_list
- |> List.length
- ;;
val it : int = 6965
> abundant_list
- |> List.sum
- ;;
val it : int = 97861532

2つの過剰数の和で書ける数の Set を 作成

>
- let abundant_set =
-     set [
-         for x in abundant_list do
-             for y in (List.filter(fun n -> n >= x) abundant_list) do
-                 let i = x + y
-                 if i <= 28123 then
-                     yield i
-     ]
- ;;

val abundant_set : Set<int> = set [24; 30; 32; 36; 38; 40; 42; 44; 48; ...]

2つの過剰数の和で書けない数の総和

> ([1..28123]
- |> List.sum) -
- (abundant_set
- |> Set.toList
- |> List.sum)
- ;;
val it : int = 4179871

C

C++

C++Builder

VC++

C#

Java

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system