Posted: Thu Aug 16, 2007 1:34 am
Problem #15 I can't get is there a formula behind it?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
DisableDebugger
test.s = ""
count.l = 0
i.q = 0
top.q = Pow(2,14)
While i.q < top
test = RSet(Bin(i),14,"0")
If CountString(test, "0") = 7 And CountString(test, "1") = 7
count = count + 1
EnableDebugger
Debug test
DisableDebugger
EndIf
i = i+1
Wend
EnableDebugger
Debug count
Code: Select all
sides bits iterat answer
1 2 4 2
2 4 16 6
3 6 64 20
4 8 256 70
5 10 1024 252
6 12 4096 924
7 14 16384 3432
....
20 40 1099511627776 ?????
When I was a commuter in New York, I walked a diagonal route across midtown Manhattan morning and evening. I used to wonder how many different paths I could find through the grid of east-west and north-south streets without taking extra steps. Eventually I grew curious enough to calculate some solutions. For a square lattice of n-by-n blocks, any minimum-length diagonal path is necessarily 2n blocks long. How many such paths are there? For the 1-by-1 lattice the answer is 2: You can go east and then north or you can go north and then east. In a two-block square there are six paths, and in a 3-by-3 block there are 20. The sequence of values I calculated begins like this: 2, 6, 20, 70, 252, 924, 3432, 12870, 48620....
Do those numbers look familiar? They should. They are prominent members of one of the most ancient and famous families of numerical sequences. And yet I did not identify them until several years later, when I had a chance to submit them to Sloane's sequence server. The answer came back immediately: They were recognized as sequence M1645, the central binomial coefficients, the numbers that run down the middle of Pascal's triangle. My reaction was "Of course! Why didn't I see it all along?" But I doubt that I would have made the connection without help.
You can either use Pascal's Triangle or combinatorics to get the answer.michaeled314 wrote:Problem #15 I can't get is there a formula behind it?
Code: Select all
n = 20
first.d = 1
Second.d = 1
For i = 1 To 2*n
first = first * i
Next
For i = 1 To n
Second = Second * i
Next
Debug first/Pow(second,2)
Here's my solution which is similar to yours, but has some differences:pdwyer wrote:AAARRRRGGHHHH the expression above works with doubles, even though 40! will lose some of it's final digits of precision, the answer still comes out correct.
I need a drinkCode: Select all
n = 20 first.d = 1 Second.d = 1 For i = 1 To 2*n first = first * i Next For i = 1 To n Second = Second * i Next Debug first/Pow(second,2)
Code: Select all
; Project Euler - Problem 15
;
; Starting in the top left corner of a 2x2 grid, there are 6 routes (without
; backtracking) to the bottom right corner.
;
; How many routes are there through a 20x20 grid?
GridSize = 20
nFactorial.d = 1
For i = 1 To GridSize
nFactorial * i
Next
nFactorial_2.d = nFactorial
For i = GridSize + 1 To GridSize << 1
nFactorial_2 * i
Next
Result.q = nFactorial_2 / (nFactorial * nFactorial)
MessageRequester("Project Euler", "Answer to problem 15: " + StrQ(Result))
Code: Select all
#gridLength=20
Global Dim paths.q(#gridLength)
;-procedures
Procedure addPathsForLevel(level.l);compute # of paths for new level based on previous level
Protected I.l,a.q,b.q
If level=1
paths(1)=2 ;2 possible paths for grid of 1x1
ProcedureReturn
EndIf
addPathsForLevel(level-1)
For I=1 To level-1
a.q=paths(I):b.q=paths(I-1)
paths(I)=a+b
Next
paths(level)=paths(level-1)*2
EndProcedure
;-Solution
addPathsForLevel(#gridLength)
result.q=0
For I=1 To #gridLength
result+paths(I)
Next
MessageRequester("Project Euler", "Answer to problem 15: " + StrQ(result))
Code: Select all
use integer;@A=split//,<>;sub R{for$i(0..80){next if$A[$i];my%t=map{$_/9
==$i/9| |$_%9==$i%9| |$_/27==$i/27&&$_%9/3==$i%9/3?$A[$_]:0=>1}0..80;R($A[
$i]=$_)for grep{!$t{$_}}1..9;return$A[$i]=0}die@A}R
Code: Select all
Procedure.l Resolve(Grid.l(1))
i.l : j.l : k.l : s.l
Dim t.l(9)
For i = 0 To 80
If Grid(i) = 0
Dim t(9)
For s = 0 To 80
If Grid(s)
If (s/9 = i/9) Or (s % 9 = i % 9) Or ((s/27 = i/27) And ((s % 9)/3 = (i % 9)/3))
t(Grid(s)) = Grid(s)
EndIf
EndIf
Next
For k = 1 To 9
If t(k) = 0
Grid(i)=k
If Resolve(Grid())
Goto nex
EndIf
EndIf
Next
Grid(i) = 0
ProcedureReturn 0
EndIf
nex:
Next
ProcedureReturn 1
EndProcedure
Dim Puzzel.s(50)
Puzzel(1) = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
Puzzel(2) = "200080300060070084030500209000105408000000000402706000301007040720040060004010003"
Puzzel(3) = "000000907000420180000705026100904000050000040000507009920108000034059000507000000"
Puzzel(4) = "030050040008010500460000012070502080000603000040109030250000098001020600080060020"
Puzzel(5) = "020810740700003100090002805009040087400208003160030200302700060005600008076051090"
Puzzel(6) = "100920000524010000000000070050008102000000000402700090060000000000030945000071006"
Puzzel(7) = "043080250600000000000001094900004070000608000010200003820500000000000005034090710"
Puzzel(8) = "480006902002008001900370060840010200003704100001060049020085007700900600609200018"
Puzzel(9) = "000900002050123400030000160908000000070000090000000205091000050007439020400007000"
Puzzel(10) = "001900003900700160030005007050000009004302600200000070600100030042007006500006800"
Puzzel(11) = "000125400008400000420800000030000095060902010510000060000003049000007200001298000"
Puzzel(12) = "062340750100005600570000040000094800400000006005830000030000091006400007059083260"
Puzzel(13) = "300000000005009000200504000020000700160000058704310600000890100000067080000005437"
Puzzel(14) = "630000000000500008005674000000020000003401020000000345000007004080300902947100080"
Puzzel(15) = "000020040008035000000070602031046970200000000000501203049000730000000010800004000"
Puzzel(16) = "361025900080960010400000057008000471000603000259000800740000005020018060005470329"
Puzzel(17) = "050807020600010090702540006070020301504000908103080070900076205060090003080103040"
Puzzel(18) = "080005000000003457000070809060400903007010500408007020901020000842300000000100080"
Puzzel(19) = "003502900000040000106000305900251008070408030800763001308000104000020000005104800"
Puzzel(20) = "000000000009805100051907420290401065000000000140508093026709580005103600000000000"
Puzzel(21) = "020030090000907000900208005004806500607000208003102900800605007000309000030020050"
Puzzel(22) = "005000006070009020000500107804150000000803000000092805907006000030400010200000600"
Puzzel(23) = "040000050001943600009000300600050002103000506800020007005000200002436700030000040"
Puzzel(24) = "004000000000030002390700080400009001209801307600200008010008053900040000000000800"
Puzzel(25) = "360020089000361000000000000803000602400603007607000108000000000000418000970030014"
Puzzel(26) = "500400060009000800640020000000001008208000501700500000000090084003000600060003002"
Puzzel(27) = "007256400400000005010030060000508000008060200000107000030070090200000004006312700"
Puzzel(28) = "000000000079050180800000007007306800450708096003502700700000005016030420000000000"
Puzzel(29) = "030000080009000500007509200700105008020090030900402001004207100002000800070000090"
Puzzel(30) = "200170603050000100000006079000040700000801000009050000310400000005000060906037002"
Puzzel(31) = "000000080800701040040020030374000900000030000005000321010060050050802006080000000"
Puzzel(32) = "000000085000210009960080100500800016000000000890006007009070052300054000480000000"
Puzzel(33) = "608070502050608070002000300500090006040302050800050003005000200010704090409060701"
Puzzel(34) = "050010040107000602000905000208030501040070020901080406000401000304000709020060010"
Puzzel(35) = "053000790009753400100000002090080010000907000080030070500000003007641200061000940"
Puzzel(36) = "006080300049070250000405000600317004007000800100826009000702000075040190003090600"
Puzzel(37) = "005080700700204005320000084060105040008000500070803010450000091600508007003010600"
Puzzel(38) = "000900800128006400070800060800430007500000009600079008090004010003600284001007000"
Puzzel(39) = "000080000270000054095000810009806400020403060006905100017000620460000038000090000"
Puzzel(40) = "000602000400050001085010620038206710000000000019407350026040530900020007000809000"
Puzzel(41) = "000900002050123400030000160908000000070000090000000205091000050007439020400007000"
Puzzel(42) = "380000000000400785009020300060090000800302009000040070001070500495006000000000092"
Puzzel(43) = "000158000002060800030000040027030510000000000046080790050000080004070100000325000"
Puzzel(44) = "010500200900001000002008030500030007008000500600080004040100700000700006003004050"
Puzzel(45) = "080000040000469000400000007005904600070608030008502100900000005000781000060000010"
Puzzel(46) = "904200007010000000000706500000800090020904060040002000001607000000000030300005702"
Puzzel(47) = "000700800006000031040002000024070000010030080000060290000800070860000500002006000"
Puzzel(48) = "001007090590080001030000080000005800050060020004100000080000030100020079020700400"
Puzzel(49) = "000003017015009008060000000100007000009000200000500004000000020500600340340200000"
Puzzel(50) = "300200000000107000706030500070009080900020004010800050009040301000702000000008006"
Answer.l
For PuzzelLoop = 1 To 50
Dim grid.l(80)
For i = 0 To 80
Grid(i)=Val(Mid(Puzzel(PuzzelLoop), i+1, 1))
Next
DisableDebugger
Resolve(Grid())
EnableDebugger
a.s = ""
For i = 0 To 80
a = a + Str(grid(i))
Next
Answer = Answer + Val(Mid(a,1,3))
Debug "Grid " + Str(PuzzelLoop) + " = " + a
Next
Debug "Answer = " + Str(answer)