Hmmm,<o:p></o:p>
<o:p> </o:p>
Forgot to update the solution for problem 31.<o:p></o:p>
<o:p> </o:p>
Please check again.<o:p></o:p>
<o:p> </o:p>
Have fun.<o:p></o:p>
<o:p> </o:p>
Kim<o:p></o:p>
<o:p> </o:p>
cur:200 100 50 20 10 5 2f<o:p></o:p>
num:“f”$cross[;]over 6#cnt:til each 1+“j”$200%cur <o:p></o:p>
num:num where 200>=result:num$6#cur <o:p></o:p>
num:num cross “f”$last cnt <o:p></o:p>
num:num where 200>=result:num$cur<o:p></o:p>
count num<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Kim Tang
Gesendet: Donnerstag, 5. Januar 2017 16:46
An: personal-kdbplus@googlegroups.com
Betreff: Re: [personal kdb+] kdb question about Euler #31<o:p></o:p>
<o:p> </o:p>
https://github.com/kimtang/kdb-euler<o:p></o:p>
<o:p> </o:p>
Kim
Sent from my iPhone<o:p></o:p>
On 5 Jan 2017, at 15:24, Kevin Smyth <kevin154@gmail.com> wrote:<o:p></o:p>
Hi wchan0284,<o:p></o:p>
<o:p> </o:p>
That’s an interesting problem, perhaps the best thing is to start by calculating all the ways of distributing 1p, 2p, 3p, etc. using each available denomination and building up a list of solutions from there - then the total number of combinations for larger amounts just involve looking up the already calculated smaller combinations. <o:p></o:p>
<o:p> </o:p>
For example, if we want to calculate the number of ways of distributing 5p we could:<o:p></o:p>
(i) Hand out 1p and look up the total number of combinations for handing out 4p<o:p></o:p>
(ii) Hand out 2p and look up the total number of combinations for handing out 3p<o:p></o:p>
(iii) Hand out 5p and look up the total number of combinations for handing out 0p (which is 1). <o:p></o:p>
<o:p> </o:p>
In terms of translating this into q we would get the below:<o:p></o:p>
<o:p> </o:p>
target:200<o:p></o:p>
coins:1 2 5 10 20 50 100 200<o:p></o:p>
// Keeps track of the number of combinations for each amount until we reach the target<o:p></o:p>
// For example combo[5] gives the number of ways of distributing 5p<o:p></o:p>
// Start with 0p = 1, initialise the rest to be 0 <o:p></o:p>
combos:1,#[target;0]<o:p></o:p>
<o:p> </o:p>
// Builds up solution out to the target amount<o:p></o:p>
{r:_[y;til 1+z];{@[x;y;+;x@y-z]}/[x;r;y]}/[combos;coins;target]<o:p></o:p>
<o:p> </o:p>
The outer loop runs across each coin denomination, the inner one builds up the number of combinations. The last entry in the result will give you your required solution. <o:p></o:p>
Some info on using / (over) in kdb+ is available here: http://code.kx.com/wiki/Reference/Slash<o:p></o:p>
<o:p> </o:p>
Thanks,<o:p></o:p>
Kevin<o:p></o:p>
<o:p> </o:p>
On 5 January 2017 at 15:10, <wchan0284@gmail.com> wrote:<o:p></o:p>
Hi kdb Group,<o:p></o:p>
<o:p> </o:p>
I am new to kdb and have been using Project Euler as a learning resource. <o:p></o:p>
<o:p> </o:p>
I have been looking at this problem and would like to know how to solve using kdb code to improve my understanding.<o:p></o:p>
<o:p> </o:p>
The problem is below, many thanks to all!<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
http://projecteuler.net/index.php?section=problems&id=31<o:p></o:p>
<o:p> </o:p>
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:<o:p></o:p>
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).It is possible to make £2 in the following way:<o:p></o:p>
1x£1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p<o:p></o:p>
How many different ways can £2 be made using any number of coins?<o:p></o:p>
–
Submitted via Google Groups