This is the second CodeSth. I just read a thread about this stuff:
12 7 13
5 6
1
The initial input is the first line you see, that is the numbers 12, 7, and 13.
I think it’s called “Subtraction Triangle,” but I am not sure. It looks a bit like a upside down version of Pascal’s Triangle, but it’s not.
Anyway, the steps are:
- Take two numbers from first number’s position: 12 and 7, abs(12 - 7) = 5, hence the first number in the second row.
- Take two numbers from second number’s position: 7 and 13, abs(7 - 13) = 6, hence the second number in the second row. The first row is finished.
- Take two numbers from first number in the second row: 5 and 6, abs(5 - 6) = 1, hence the only number in the third row. The second row is finished, so is the triangle.
Basically, the rules are: each time you take two numbers, subtract them, put the abs(new number) into next row, move onto next row when there are no more two numbers can be taken. End the process when the row has only one number.
Bash
ReplyDelete#!/bin/bash
num_set=($@)
pad=0
while printf "%*s" $pad ''
printf "%2d " ${num_set[@]}
echo
((${#num_set[@]} > 1))
do
new_set=()
for ((i = 0; i < ${#num_set[@]} - 1; i++)); do
d=$((num_set[i] - num_set[i + 1]))
new_set+=($((d > 0 ? d : -d)))
done
num_set=(${new_set[@]})
let pad+=2
done