Christmas trees from factored integers
(I’ll warn you: these are, at best, a drunk geometrician’s interpretation of modern art’s idea of a Christmas tree — in other words, triangle-looking things that may or may not remind you of pine trees if you’re in an excellent mood.)
Have you noticed how hard it is to hit a large prime number by chance just by smashing your keyboard?
Well, coreutils’ factor can handle large-ish numbers — and it’s fast. Try it.
Here are a few examples.
time <<<"2^126 + 1234567" bc | factor
85070591730234615865843651857943287431: 35141653447 2420790810498900651396081473
time <<<"2^142 + 1424242424242424242421" bc | factor
5575186299632655785385353810586332800737525: 3 5 5 1731893 42921714752837161690592154061760419
Of course, speed depends on the factors. This is instantaneous:
time <<<"2^200 + 13" bc | factor
1606938044258990275541962092341162602522202993782792835301389: 29 83 223 2993768258608561865601193254243811682522021893883484149
Whereas this will take several seconds until it reaches the 397380968069 factor:
time <<<"2^200 + 17" bc | factor
1606938044258990275541962092341162602522202993782792835301393: 3 7 13 397380968069 14812536118535396812851350993956040610411539789
Now let’s use it to build some Christmas trees.
I’ve just written the below.
_divider() { printf "%*s\n" "$(wc -L)" " " | sed "y/ /━/" ;} _aligner() { sed -E "s/ $/ /; s/^([^ ]+)( +)( .*)/\2\1\3/" ;} _factree() { for ((i="${#1}"; i>=0; i--)) do <<<"$1" sed -E "s/.{$i}$//" | factor done | column -ts: ;}
And for the main function, two options — different styles, same output.
#1: Ultimate Bashing
factrees() { echo "$(_factree "$1")" # 'Useless echo', ShellCheck? Not quite. <<<"$_" _divider | cat # (And a useful cat this is.) <<<"$_" _aligner ;}
#2: Our good old tee’ing
factrees() { _factree "$1" | tee >(_aligner) \ >(_divider) ;}
Let’s run it on some random number that I definitely didn’t modify to add a bunch of 42s:
factrees 742634299425742184211428442
7 7
74 2 37
742 2 7 53
7426 2 47 79
74263 7 103 103
742634 2 19 19543
7426342 2 7 7 11 83 83
74263429 17 2029 2153
742634299 11 3659 18451
7426342994 2 7 23 3931 5867
74263429942 2 13 71 40229377
742634299425 3 5 5 47 210676397
7426342994257 11 83 8134001089
74263429942574 2 97 6451 59339821
742634299425742 2 371317149712871
7426342994257421 13 1669 396377 863509
74263429942574218 2 331 367 1709 178858213
742634299425742184 2 2 2 62311 162517 9166879
7426342994257421842 2 17 23 157 60487912703483
74263429942574218421 31 1039 1347331 1711289399
742634299425742184211 3 247544766475247394737
7426342994257421842114 2 13 4831 72661 813697998479
74263429942574218421142 2 3 12377238323762369736857
742634299425742184211428 2 2 73 701 3628057273492575109
7426342994257421842114284 2 2 3 1013 84181 7257219048839569
74263429942574218421142844 2 2 7 19 55813618213 2501054722759
742634299425742184211428442 2 3 37 307 10896415462419552545873
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
7 7
74 2 37
742 2 7 53
7426 2 47 79
74263 7 103 103
742634 2 19 19543
7426342 2 7 7 11 83 83
74263429 17 2029 2153
742634299 11 3659 18451
7426342994 2 7 23 3931 5867
74263429942 2 13 71 40229377
742634299425 3 5 5 47 210676397
7426342994257 11 83 8134001089
74263429942574 2 97 6451 59339821
742634299425742 2 371317149712871
7426342994257421 13 1669 396377 863509
74263429942574218 2 331 367 1709 178858213
742634299425742184 2 2 2 62311 162517 9166879
7426342994257421842 2 17 23 157 60487912703483
74263429942574218421 31 1039 1347331 1711289399
742634299425742184211 3 247544766475247394737
7426342994257421842114 2 13 4831 72661 813697998479
74263429942574218421142 2 3 12377238323762369736857
742634299425742184211428 2 2 73 701 3628057273492575109
7426342994257421842114284 2 2 3 1013 84181 7257219048839569
74263429942574218421142844 2 2 7 19 55813618213 2501054722759
742634299425742184211428442 2 3 37 307 10896415462419552545873
Whoa!
But then I thought: instead of just realigning, we could build a different second tree by trimming the number from the left.
New numbers, new factors.
So let’s do that:
# These two remain the same _divider() { printf "%*s\n" "$(wc -L)" " " | sed "y/ /━/" ;} _aligner() { sed -E "s/ $/ /; s/^([^ ]+)( +)( .*)/\2\1\3/" ;} # And we adapt _factree() so that it can trim from either side # trimming itself could be done either with sed... _factree() { local t="$1" n="$2" d="${#2}" # Option A for ((i="$((d-1))"; i>=0; i--)) do ((t==1)) && : ".{$i}$" || : "^.{$i}" <<<"$n" sed -E "s/$_//" | factor done | column -ts: ;} # ...or with Bash's substring expansion _factree() { local t="$1" n="$2" d="${#2}" # Option B for ((i="$((d-1))"; i>=0; i--)) do <<<"${n:$((t==1?0:i)):$((d-i))}" factor done | column -ts: ;} # The main function is then straightforward factrees() { _factree 1 "$1" | tee >(_divider) _factree 2 "$1" | _aligner ;} factrees 742634299425742184211428442
7 7
74 2 37
742 2 7 53
7426 2 47 79
74263 7 103 103
742634 2 19 19543
7426342 2 7 7 11 83 83
74263429 17 2029 2153
742634299 11 3659 18451
7426342994 2 7 23 3931 5867
74263429942 2 13 71 40229377
742634299425 3 5 5 47 210676397
7426342994257 11 83 8134001089
74263429942574 2 97 6451 59339821
742634299425742 2 371317149712871
7426342994257421 13 1669 396377 863509
74263429942574218 2 331 367 1709 178858213
742634299425742184 2 2 2 62311 162517 9166879
7426342994257421842 2 17 23 157 60487912703483
74263429942574218421 31 1039 1347331 1711289399
742634299425742184211 3 247544766475247394737
7426342994257421842114 2 13 4831 72661 813697998479
74263429942574218421142 2 3 12377238323762369736857
742634299425742184211428 2 2 73 701 3628057273492575109
7426342994257421842114284 2 2 3 1013 84181 7257219048839569
74263429942574218421142844 2 2 7 19 55813618213 2501054722759
742634299425742184211428442 2 3 37 307 10896415462419552545873
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2 2
42 2 3 7
442 2 13 17
8442 2 3 3 7 67
28442 2 14221
428442 2 3 7 101 101
1428442 2 17 42013
11428442 2 73 78277
211428442 2 105714221
4211428442 2 13 53 881 3469
84211428442 2 23 25633 71419
184211428442 2 9199 10012579
2184211428442 2 1092105714221
42184211428442 2 21092105714221
742184211428442 2 3 3 139 449 660660079
5742184211428442 2 2871092105714221
25742184211428442 2 17 101 433 17312397161
425742184211428442 2 212871092105714221
9425742184211428442 2 271 201203 86433444017
99425742184211428442 2 2789 17824622119794089
299425742184211428442 2 167 896484258036561163
4299425742184211428442 2 101603 21157966507801007
34299425742184211428442 2 7 2449958981584586530603
634299425742184211428442 2 83 3393839 4103159 274395287
2634299425742184211428442 2 37 294181 33627163 3598558511
42634299425742184211428442 2 13 115778638993 14163068087569
742634299425742184211428442 2 3 37 307 10896415462419552545873
Whoa!
Enjoy smashing the keyboard to generate integers to create your, uh, Christmas trees.
And once 🎄 Christmas is over, look forward ⏩ to the New Year:
factfwd() { _factree 1 "$1" _factree 2 "$1" | tac | sed 1d ;} factfwd 742634299425742184211428442
7 7 74 2 37 742 2 7 53 7426 2 47 79 74263 7 103 103 742634 2 19 19543 7426342 2 7 7 11 83 83 74263429 17 2029 2153 742634299 11 3659 18451 7426342994 2 7 23 3931 5867 74263429942 2 13 71 40229377 742634299425 3 5 5 47 210676397 7426342994257 11 83 8134001089 74263429942574 2 97 6451 59339821 742634299425742 2 371317149712871 7426342994257421 13 1669 396377 863509 74263429942574218 2 331 367 1709 178858213 742634299425742184 2 2 2 62311 162517 9166879 7426342994257421842 2 17 23 157 60487912703483 74263429942574218421 31 1039 1347331 1711289399 742634299425742184211 3 247544766475247394737 7426342994257421842114 2 13 4831 72661 813697998479 74263429942574218421142 2 3 12377238323762369736857 742634299425742184211428 2 2 73 701 3628057273492575109 7426342994257421842114284 2 2 3 1013 84181 7257219048839569 74263429942574218421142844 2 2 7 19 55813618213 2501054722759 742634299425742184211428442 2 3 37 307 10896415462419552545873 42634299425742184211428442 2 13 115778638993 14163068087569 2634299425742184211428442 2 37 294181 33627163 3598558511 634299425742184211428442 2 83 3393839 4103159 274395287 34299425742184211428442 2 7 2449958981584586530603 4299425742184211428442 2 101603 21157966507801007 299425742184211428442 2 167 896484258036561163 99425742184211428442 2 2789 17824622119794089 9425742184211428442 2 271 201203 86433444017 425742184211428442 2 212871092105714221 25742184211428442 2 17 101 433 17312397161 5742184211428442 2 2871092105714221 742184211428442 2 3 3 139 449 660660079 42184211428442 2 21092105714221 2184211428442 2 1092105714221 184211428442 2 9199 10012579 84211428442 2 23 25633 71419 4211428442 2 13 53 881 3469 211428442 2 105714221 11428442 2 73 78277 1428442 2 17 42013 428442 2 3 7 101 101 28442 2 14221 8442 2 3 3 7 67 442 2 13 17 42 2 3 7 2 2
📆 2025-W51-7📆 2025-12-21