Created
October 19, 2014 13:43
-
-
Save MeilCli/88bf12640af55a703d24 to your computer and use it in GitHub Desktop.
ゴリ押し
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace AjinoriNoTyousen { | |
public class Program { | |
//Q. 正の整数であって、その正の約数のうち4で割った余りが2でないようなものの総和が1000であるものをすべて求めよ. | |
public static void Main(string[] args) { | |
Func<int,int> sum = x => { | |
int s = 0; | |
for(int i = 1;i <= x;i = i + 4) { | |
s = s + (x % i == 0 ? i : 0); | |
s = s + ((i + 2) <= x && x % (i + 2) == 0 ? (i + 2) : 0); | |
s = s + ((i + 3) <= x && x % (i + 3) == 0 ? (i + 3) : 0); | |
} | |
return s; | |
}; | |
Func<int,int[]> split = x => { | |
var list = new List<int>(); | |
for(int i = 1;i <= x;i++) { | |
if(i % 2 == 0 && i % 4 != 00) { | |
continue; | |
} | |
if(x % i != 0) { | |
continue; | |
} | |
list.Add(i); | |
} | |
return list.ToArray(); | |
}; | |
//検索範囲は[条件に合う]1+(約数)+n=1000のnまで | |
//ここでは簡易的に1+1000>1000である1000に設定 | |
for(int n = 1;n <= 1000;n++) { | |
int sn = sum(n); | |
if(sn == 1000) { | |
Console.Out.Write(n); | |
Console.Out.Write(" : "); | |
Console.Out.WriteLine(string.Join("+",split(n))); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
448 : 1+4+7+8+16+28+32+56+64+112+224+448 | |
796 : 1+4+199+796 | |
続行するには何かキーを押してください . . . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
100000までの探索でも同じ解