ProjectEuler 125

http://projecteuler.net/index.php?section=problems&id=125

Problem 125

04 August 2006

The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 6^2+7^2+8^2+9^2+10^2+11^2+12^2

There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 0^2+1^2 was not been included as this problem is concerned with the squares of positive integers.

Find the sum of all the numbers less than 108 that are both palindromic and can be written as the sum of consecutive squares.

Answer:
2906969179

Sub Main() Dim sum, i As Long, j As Long, k As Long, dic As Object, max As Long Set dic = CreateObject("scripting.dictionary") For i = 1 To 9999 k = i * i For j = i + 1 To 10000 k = k + j * j If k >= 10 ^ 8 Then Exit For If k = StrReverse(k) Then If Not dic.exists(k) Then If k > max Then max = k dic(k) = k & "=" & Join(Evaluate("transpose(row(" & i & ":" & j & "))"), "^2+") & "^2" sum = sum + k End If End If Next Next Debug.Print "Sum=" & sum & vbCrLf & "Count=" & dic.Count & vbCrLf & "Max:" & dic(max) Set dic = Nothing End Sub

It returns:

Sum=2906969179
Count=166
Max:97299279=312^2+313^2+314^2+315^2+316^2+317^2+318^2+319^2+320^2+321^2+322^2+323^2+324^2+325^2+326^2+327^2+328^2+329^2+330^2+331^2+332^2+333^2+334^2+335^2+336^2+337^2+338^2+339^2+340^2+341^2+342^2+343^2+344^2+345^2+346^2+347^2+348^2+349^2+350^2+351^2+352^2+353^2+354^2+355^2+356^2+357^2+358^2+359^2+360^2+361^2+362^2+363^2+364^2+365^2+366^2+367^2+368^2+369^2+370^2+371^2+372^2+373^2+374^2+375^2+376^2+377^2+378^2+379^2+380^2+381^2+382^2+383^2+384^2+385^2+386^2+387^2+388^2+389^2+390^2+391^2+392^2+393^2+394^2+395^2+396^2+397^2+398^2+399^2+400^2+401^2+402^2+403^2+404^2+405^2+406^2+407^2+408^2+409^2+410^2+411^2+412^2+413^2+414^2+415^2+416^2+417^2+418^2+419^2+420^2+421^2+422^2+423^2+424^2+425^2+426^2+427^2+428^2+429^2+430^2+431^2+432^2+433^2+434^2+435^2+436^2+437^2+438^2+439^2+440^2+441^2+442^2+443^2+444^2+445^2+446^2+447^2+448^2+449^2+450^2+451^2+452^2+453^2+454^2+455^2+456^2+457^2+458^2+459^2+460^2+461^2+462^2+463^2+464^2+465^2+466^2+467^2+468^2+469^2+470^2+471^2+472^2+473^2+474^2+475^2+476^2+477^2+478^2+479^2+48
0^2+481^2+482^2+483^2+484^2+485^2+486^2+487^2+488^2+489^2+490^2+491^2+492^2+493^2+494^2+495^2+496^2+497^2+498^2+499^2+500^2+501^2+502^2+503^2+504^2+505^2+506^2+507^2+508^2+509^2+510^2+511^2+512^2+513^2+514^2+515^2+516^2+517^2+518^2+519^2+520^2+521^2+522^2+523^2+524^2+525^2+526^2+527^2+528^2+529^2+530^2+531^2+532^2+533^2+534^2+535^2+536^2+537^2+538^2+539^2+540^2+541^2+542^2+543^2+544^2+545^2+546^2+547^2+548^2+549^2+550^2+551^2+552^2+553^2+554^2+555^2+556^2+557^2+558^2+559^2+560^2+561^2+562^2+563^2+564^2+565^2+566^2+567^2+568^2+569^2+570^2+571^2+572^2+573^2+574^2+575^2+576^2+577^2+578^2+579^2+580^2+581^2+582^2+583^2+584^2+585^2+586^2+587^2+588^2+589^2+590^2+591^2+592^2+593^2+594^2+595^2+596^2+597^2+598^2+599^2+600^2+601^2+602^2+603^2+604^2+605^2+606^2+607^2+608^2+609^2+610^2+611^2+612^2+613^2+614^2+615^2+616^2+617^2+618^2+619^2+620^2+621^2+622^2+623^2+624^2+625^2+626^2+627^2+628^2+629^2+630^2+631^2+632^2+633^2+634^2+635^2+636^2+637^2+638^2+639^2+640^2+641^2+642^2+643^2+644^2+645^2+646^2+647^2+648^2+649^2+650^2
+651^2+652^2+653^2+654^2+655^2+656^2+657^2+658^2+659^2+660^2+661^2+662^2+663^2+664^2+665^2+666^2+667^2+668^2+669^2+670^2+671^2+672^2+673^2+674^2+675^2+676^2+677^2+678^2+679^2+680^2+681^2+682^2+683^2+684^2+685^2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值