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 }

 

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

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

 

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

 

 

 

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

Can you explain the start of the code? :

q)-1 "0123456789"

 

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

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