Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- kdb+ and q
- :
- Re: Q For Problems - Episode 1

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Q For Problems - Episode 1

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

Hi everyone,

Please check out episode 1 for my new Q For Problems video series.

This series will use the Q language to solve various programming problems.

Episode 1 covers problem 1 from the Project Euler problem set. Project Euler - Problem 1

Feel free to share your own solutions and ideas in the comments as I would be very interested to see what other people come up with.

Thanks

9 REPLIES 9

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

Nice video, I didnt compare performance but here's what I came up with initially;

f:{sum distinct (3*1+til n div 3),5*1+til (n:x-1) div 5}

The same idea with the lists but I have joined and distinct on the lists rather than calculating for 15.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

Nice one!

I ran the perf test and these were the results compared to s1 and s2.

Below 1000:

f n total average mem

---------------------------------------------------------------------

(`s1;1000;3 5) 100000 0D00:00:01.000680000 0D00:00:00.000010007 41120

(`s2;999;3 5) 100000 0D00:00:00.061489000 0D00:00:00.000000615 384

(`lfox;1000) 100000 0D00:00:00.261888000 0D00:00:00.000002619 17536

Below 100000:

f n total average mem

-----------------------------------------------------------------------

(`s1;100000;3 5) 1000 0D00:00:01.112241000 0D00:00:00.001112241 5243040

(`s2;99999;3 5) 1000 0D00:00:00.000624000 0D00:00:00.000000624 384

(`lfox;100000) 1000 0D00:00:00.149855000 0D00:00:00.000149855 1310848

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

Nice, thanks. Somewhere in between then, maybe the join/distinct isn't very performant then

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

Thank your for taking the time to explain this to me Jonathan! Very well organized video.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

Not as fast as `s2`

but much faster than `s1`

:

```
q){til each y}[10;3 5]
0 1 2
0 1 2 3 4
q){(y-1)=til each y}[10;3 5]
001b
00001b
q){(x-1)#'(y-1)=til each y}[10;3 5]
001001001b
000010000b
q){any(x-1)#'(y-1)=til each y}[10;3 5]
001011001b
q){where any(x-1)#'(y-1)=til each y}[10;3 5]
2 4 5 8
q){1+where any(x-1)#'(y-1)=til each y}[10;3 5]
3 5 6 9
q){sum 1+where any(x-1)#'(y-1)=til each y}[10;3 5]
23
q)\ts:10000 s1[1000;3 5]
141 41344
q)\ts:10000 s2[1000;3 5]
12 960
q)\ts:10000 s3[1000;3 5]
32 8480
```

Of course there is no need to add 1 to each index before summing. For high values of `x`

, just add in to the sum of the indices their count.

```
q)s4:{sum(count;sum)@\:where any(x-1)#'(y-1)=til each y}
q)s4[1000;3 5]
233168
q)\ts:10000 s4[1000;3 5]
30 5408
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Thursday

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Friday

Not as mathematically sophisticated as `s2`

, but exploits the speed of the interpreter’s mapping of bits to indices.

Of course the bit pattern repeats in groups of 15, so one can shorten the arguments to `any`

, but it takes a large value of `x`

for this to pay off.

```
q)s5:{sum(sum;count)@\:where (x-1)#any prd[y]#'(y-1)=til each y}
q)\ts:1000 s4[1000000;3 5]
745 5243168
q)\ts:1000 s5[1000000;3 5]
635 5243168
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

yesterday

Great video, looking forward to episode 2!

Head of Evangelism - KX

Related Content

- What is an array-programming language? in General
- Q For Problems - Episode 1 in kdb+ and q
- Vector programming in q – online workshop – Sun 25 Sep in New kdb+/q users question forum
- Dave Thomas on Tech Lead Journal podcast in General
- How do you start thinking in vectors? in New kdb+/q users question forum

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Resources

Popular Links

Follow Us

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.