Craig Russell

Web n That

I ♡… the web… php… html… css… linux… shell scripting… folding paper… dog walking… sandwiches… being outside… and most of all @vicuol ☺

Shell Script to find Square-Triagular Numbers

July 29th, 2010

Given this puzzle from Matt Parker, I wrote a swift little shell script to find the solution.

1
2
3
4
5
6
7
8
9
for i in {1..100}; do
    I=$[i*i]
    for j in {1..100}; do
        J=$[(j*(j-1))/2]
        if [[ $I == $J ]]; then
            echo $I
        fi
    done
done

Not the most efficient solution, but still a fun exercise.

  • http://www.wplancer.com Banago

    Can you run that script directly on terminal. I’m on Ubunut, but not very much into shell. Also, what language would that be?

  • Craig

    @Banago
    It’s in bash (default shell language in Ubuntu).
    you can run it directly in terminal, if you separate lines with the semi-colon.
    But you’re probably best pasting it into a text file, making it executable with chmod, then running that from terminal.