# Challenge 1 - Triangle Font

**URL:** <https://forum.kx.com/t/challenge-1-triangle-font/13816>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [January 30, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-1-triangle-font/13816 "2023-01-30T00:00:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [January 30, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-1-triangle-font/13816/1 "2023-01-30T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/challenge-1-triangle-font](https://learninghub.kx.com/forums/topic/challenge-1-triangle-font)

Hi all ! I want to introduce my first challenge Triangle Font.

I initially solved it in python as I am a bit more confident in that language then I attempted to solve it in q.

Problem 1 Triangle Font

You are given a positive integer?X. Print a numerical triangle of height?X - 1?like the one below:

1

22

333

4444

55555

..

Python Solution:

for i in range(1,int(input())): print((pow(10, i)//9)\*i)

This is where Ive got to in q:

```
1 "Type a number: "; //Prompt for user input 
input: read0 0; //Save user input as variable 
i: 1; //Assign variable i as 1 
output:{[input] //Function with input as parameter 
          each[(floor((10 xexp i)%9)*i);input] //floor = to round down, 10 xexp i = 10 to the power of i, divide by 9, multiplied by i 
          i: i+1; //Increment i by 1 }
```

&nbsp;

I am trying to imitate the iterating of the for loop by using each while also incrementing i.

This is how I visualised my approach, but it is throwing a type error.

Hoping I can get some suggestions on how else I could approach this problem, and any advice would be greatly appreciated.

Thanks in advance,

Megan

---

<div class="post-metadata">

**Author:** ![smdesai](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@smdesai](https://forum.kx.com/u/smdesai)\
**Post date:** [March 19, 2024, 11:00am UTC](https://forum.kx.com/t/challenge-1-triangle-font/13816/2 "2024-03-19T11:00:54Z")

</div>

Hello!  
I saw your python solution and came up with the following.

input: “I”$read0 0

output:{((10 xexp x) div 9) \* x} each 1 + til input show each

“j”$output

&nbsp;

---

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [March 19, 2024, 11:00am UTC](https://forum.kx.com/t/challenge-1-triangle-font/13816/3 "2024-03-19T11:00:54Z")

</div>

Thank you for your reply! I forgot about the casting.

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [March 19, 2024, 11:00am UTC](https://forum.kx.com/t/challenge-1-triangle-font/13816/4 "2024-03-19T11:00:54Z")

</div>

Perfect! What’s more effective than one line of code?

Can you explain the start of the code? :

```
q)-1 "0123456789"
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![baichen](https://avatars.discourse-cdn.com/v4/letter/b/ecccb3/32.png) [@baichen](https://forum.kx.com/u/baichen)\
**Post date:** [February 2, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-1-triangle-font/13816/5 "2023-02-02T00:00:00Z")

</div>

```
q){b:first x+1;b#x+1}[8;1] 
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8
```

---

<div class="post-metadata">

**Author:** ![jfealy](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jfealy](https://forum.kx.com/u/jfealy)\
**Post date:** [February 2, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-1-triangle-font/13816/6 "2023-02-02T00:00:00Z")

</div>

Another alternative

```
q)ts:10000 {x#'x} 1+til 100 
207 63616 
q)ts:10000 {sums[-1_t]_where t:til 1+x}100 
106 130112 
q){x#'x}[1+til 100]~{sums[-1_t]_where t:til 1+x}100 
1b
```

However, when applied to a large vector, performance drops off as cut is expensive

```
q)ts {x#'x} 1+til 50000 
4099 14763251840 
q)ts {sums[-1_t]_where t:til 1+x}50000 
6454 31943645248
```

&nbsp;
