僩僢僾儁乕僕偵栠傞
師偺C#偺僒儞僾儖傊
慜偺C#偺僒儞僾儖傊
C儅僈揹擼僋儔僽(戞097夞) 徃弴慺悢
栤戣
2113偼慺悢偩
偱傕偙偙偱偼慺悢偺奺寘傪峔惉偡傞悢帤偑丄塃偵峴偔偵廬偭偰戝偒偔側傞傕偺丄偮傑傝徃弴慺悢偩偗傪峫偊偨偄丅
偲偡傞偲13偵偼帒奿偑偁傞偑11偼幐奿偡傞袀茖厒虘f悢偼偡傋偰徃弴慺悢偱偁傞偲偡傞
偝偰丄偁傞堎側傞嶰偮偺徃弴慺悢傪懌偟崌傢偣偨偲偙傠丄偦偺寢壥傕徃弴慺悢偵側偭偰偄偨偲偄偆丅
19+89+359=467
偙偺傛偆側3悢偺慻偼偙偺椺傪娷傔偰壗捠傝偁傞偩傠偆偐
僜乕僗
using System;
using System.Collections.Generic;
class Program
{
const int Jyougen = 123456789;
static int[] SosuuArr;
//僄儔僩僗僥僱僗偺饪偱忋尷埲壓偺慺悢偺攝楍傪曉偡
static void Eratosthenes()
{
var IsSosuuArr = new System.Collections.BitArray(Jyougen + 1);
for (int I = 2; I <= IsSosuuArr.Count - 1; I++) {
IsSosuuArr[I] = true;
}
for (int I = 2; I * I <= IsSosuuArr.Count - 1; I++) {
if (IsSosuuArr[I] == false) continue;
for (int J = I * 2; J <= IsSosuuArr.Count - 1; J += I) {
IsSosuuArr[J] = false;
}
}
var SosuuList = new List<int>();
for (int I = 2; I <= IsSosuuArr.Count - 1; I++) {
if (IsSosuuArr[I]) SosuuList.Add(I);
}
SosuuArr = SosuuList.ToArray();
}
static void Main()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
Eratosthenes();
Console.WriteLine("{0}埲壓偺慺悢偼{1}屄", Jyougen, SosuuArr.Length);
int[] AscSosuuArr = Array.FindAll(SosuuArr, X => IsAscSosuu(X));
Console.WriteLine("{0}埲壓偺徃弴慺悢偼{1}屄", Jyougen, AscSosuuArr.Length);
int AnswerCnt = 0;
for (int I = 0; I <= AscSosuuArr.GetUpperBound(0); I++) {
for (int J = I + 1; J <= AscSosuuArr.GetUpperBound(0); J++) {
for (int K = J + 1; K <= AscSosuuArr.GetUpperBound(0); K++) {
int wkSum = AscSosuuArr[I] + AscSosuuArr[J] + AscSosuuArr[K];
int StaInd = K + 1;
int wkInd =
Array.BinarySearch(AscSosuuArr, StaInd, AscSosuuArr.Length - StaInd, wkSum);
if (wkInd >= 0) {
Console.WriteLine("夝{0}傪敪尒丅{1}+{2}+{3}={4}",
++AnswerCnt, AscSosuuArr[I], AscSosuuArr[J], AscSosuuArr[K], wkSum);
}
}
}
}
Console.WriteLine("宱夁帪娫={0}", sw.Elapsed);
}
//徃弴慺悢偐傪敾掕
static bool IsAscSosuu(int pTarget)
{
var NumList = new List<int>();
int CopiedVal = pTarget;
do {
NumList.Add(CopiedVal % 10);
CopiedVal /= 10;
} while (CopiedVal > 0);
NumList.Reverse();
for (int I = 0; I <= NumList.Count - 1 - 1; I++) {
if (NumList[I] >= NumList[I + 1])
return false;
}
return true;
}
}
幚峴寢壥
夝290傪敪尒丅389+479+12589=13457
夝291傪敪尒丅389+1279+1789=3457
夝292傪敪尒丅389+1289+1789=3467
夝293傪敪尒丅389+1489+1579=3457
夝294傪敪尒丅389+1489+2689=4567
夝295傪敪尒丅389+1789+2389=4567
夝296傪敪尒丅1289+1489+1789=4567
宱夁帪娫=00:00:41.0077973
夝愢
僄儔僩僗僥僱僗偺饪偱123456789埲壓偺慺悢傪楍嫇偟偰偐傜丄
徃弴慺悢偐傪敾掕偟偰傑偡丅
怺偝桪愭扵嶕傪巊偭偰丄
2偺9忔-1= 511捠傝偺
徃弴側悢傪楍嫇偟偰偐傜丄帋偟妱傝偱慺悢敾掕偟偨傎偆偑
崅懍偵張棟偱偒偦偆偱偡偹丅