README: Factorial quick chart with sed & bc:

Hi all,
While doing some checks I found a kind of interesting arithmetic factorial chart with sed, sharing this may be simple but thought to share,

# n=20;for i in `seq $n`;do printf "`seq $i|xargs|sed 's/ /*/g'`= ";echo "`seq $i|xargs|sed 's/ /*/g'`"| bc;done
1= 1
1*2= 2
1*2*3= 6
1*2*3*4= 24
1*2*3*4*5= 120
1*2*3*4*5*6= 720
1*2*3*4*5*6*7= 5040
1*2*3*4*5*6*7*8= 40320
1*2*3*4*5*6*7*8*9= 362880
1*2*3*4*5*6*7*8*9*10= 3628800
1*2*3*4*5*6*7*8*9*10*11= 39916800
1*2*3*4*5*6*7*8*9*10*11*12= 479001600
1*2*3*4*5*6*7*8*9*10*11*12*13= 6227020800
1*2*3*4*5*6*7*8*9*10*11*12*13*14= 87178291200
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15= 1307674368000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16= 20922789888000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17= 355687428096000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18= 6402373705728000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19= 121645100408832000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20= 2432902008176640000
# n=20;for i in `seq $n -1 1`;do printf "`seq $i|xargs|sed 's/ /*/g'`= ";echo "`seq $i|xargs|sed 's/ /*/g'`"| bc;done
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20= 2432902008176640000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19= 121645100408832000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18= 6402373705728000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17= 355687428096000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16= 20922789888000
1*2*3*4*5*6*7*8*9*10*11*12*13*14*15= 1307674368000
1*2*3*4*5*6*7*8*9*10*11*12*13*14= 87178291200
1*2*3*4*5*6*7*8*9*10*11*12*13= 6227020800
1*2*3*4*5*6*7*8*9*10*11*12= 479001600
1*2*3*4*5*6*7*8*9*10*11= 39916800
1*2*3*4*5*6*7*8*9*10= 3628800
1*2*3*4*5*6*7*8*9= 362880
1*2*3*4*5*6*7*8= 40320
1*2*3*4*5*6*7= 5040
1*2*3*4*5*6= 720
1*2*3*4*5= 120
1*2*3*4= 24
1*2*3= 6
1*2= 2
1= 1
#

Just replace the value of n , to the desired factorial and it ll construct the chart. Below is an example of factorial 20 's chart:

n=20;for i in `seq $n`;do printf "`seq $i|xargs|sed 's/ /*/g'`= ";echo "`seq $i|xargs|sed 's/ /*/g'`"| bc;done

Enjoy!. Have fun!.

2 Likes

Nice example.
Buts its some more easy to read when using $(code) instead of `code`

n=20;for i in $(seq $n);do printf "$(seq $i|xargs|sed 's/ /*/g')= ";echo "$(seq $i|xargs|sed 's/ /*/g')"| bc;done

bc also needed to be installed manually on Ubuntu

1 Like

Inspirational, here is an awk version of the top half:

awk -v n=20 'BEGIN {t=s=1; for(i=1;i<=n;i++) {t*=i; if(i>1) s=s"*"i; print s"= "t}}'
1 Like

awk does not work when n>170 , not sure how long bc goes, but n=1000 works fine :slight_smile:

1 Like

Jotne, thanks for checking & suggestions:
Yes, awk can goes upto 170 only,

  • I have cheked with hp-ux version and linux suse versions too:

With the awk version Factorial:
170! Gives:

168*169*170= 725741561530799404539963...48167424" [ length=307 column long.] 

171! Gives: infinity.

168*169*170*171= inf

Scrutinizer, thanks ,awk code works great thanks for making the awk version of factorial, seems there is a limitation of 171th calculation. But it is good to have the code .Thanks,

  • For the first unix command based code with sed & bc:
    Out of curiosity I tried Jotne's remark for n=1000 value and it worked like a charm .

Also wondered when I put it n=2000, we know factorial of 2000 is going to be huge, and this too worked on Suse 11.2 VM perfectly. It was kind of grabled the display at the 19xx as it was processing so quickly.

---------- Post updated at 06:08 PM ---------- Previous update was at 05:56 PM ----------

All, Out of curiosity I increased factorial value , n=2000 and it worked to:

n=2000;for i in `seq $n`;do printf "`seq $i|xargs|sed 's/ /*/g'`= ";echo "`seq $i|xargs|sed 's/ /*/g'`"| bc;done

Showing factorial of 2000 below :
on Linux Suse 11.2 VM , shell = /bin/bash

GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)
<.......> 

represents the range of the display:

2000! =
1*2*3*4*5*6*7*8*9*<.......>1994*1995*1996*1997*1998*1999*2000= 33162750924506332411753933805763240382811172081057803945719354370603\
80779056008224002732308597325922554023529412258341092580848174152937\
96131386633526343688905634058556163940605117252571870647856393544045\
40524395746703767410872297043468415834375243158087753364512748799543\
68592474080324089465615072332506527976557571796715367186893590561128\
15871601717232657156110004214012420433842573712700175883547796899921\
28352899666585340557985490365736635013338655040117201215263548803826\
81521522469209952060315644185654806759464970515522882052348999957264\
50814065536678969532101467622671332026831552205194494461618239275204\
02652972263150257475204829606475092739416585628353177957448287631459\
64503739913273341772636088524900935066216101444597094127078213137325\
63831572302019949914958316470942774473870327985549674298608839376326\
82415247883438746959582925774057453983750158581546813629421794997239\
98135994810165565638760342273129122503847098729096266224619710766059\
31550201895135583165357871492290916779049702247094611937607785165110\
68443225590564873626653037738465039078804952460071254940261456607225\
41363027549136715834060978310749452822174907813477096932415561113398\
28051358600690594619965257310741177081519922564516778571458056602185\
65476095237746301667942248844448579834980154803262082989096585738175\
18886193766928282798884535846398965942139529844652910920091037100461\
49449915828588050761867924946385180879874512891408019340074625920057\
09872957859964365065589561241023101869055606030878362911050560124590\
89983834107993679020520768586691834779065585447001486926569246319333\
37612428097420067172846361939249698628468719993450393889367270487127\
17273456170035486747750910295552395354794110742191330135681954109194\
14627664175421615876252628580898012224438902486771820549594157519917\
01271767571787495861619665931878855141835782092601482071777331735396\
03430496908207058995870138198081303559016076290838857456128821769813\
61824835767392183031184147191339868928423440007792466912097667316514\
33494437473235636572048844478331854941693030124531676232745367879322\
84747382448509228313995250973250597912703104768360148119110222925337\
26976938236700575656124002905760438528529029376064795334581796661238\
39605262549107186663869354766108455046198102084050635827676526589492\
39324951968595417167241932953068367349554400458635983816104305944982\
66275306054235807558941082788804278259510898806354105679179509740177\
80688782869810219010900148352061688883720250310665922068601483649830\
53278208826353655804360568678128416921713304714117631217589577712263\
75847531235172309905498292101346873042058980144180638753826641698977\
04237759406280877253702265426530580862379301422675821187143502918637\
63634030017325181826207603974736959520264263236414544685111342720215\
04583838510101369413130348562219166316238926327658153550112763078250\
59969158824533457435437863683173730673296589355199694458236873508830\
27865770087974988999234355556624068283476378468518384497364887395247\
51032242221105612012958296571913681086938254757641188868793467251912\
46192151144738836269591643672490071653428228152661247800463922544945\
17036372362794075778454209104830546165619062217428698160297332404652\
02019928138548826819510072828697010707375009276664875021747753727423\
51508748246720274170031581122805896178122160747437947510950620938556\
67458125251837668215771280786149925587613235295042234638787895485088\
57644661362903941276659780442020922813379871159008962648789424132104\
54925003566670632909441579372986743421470507213588932019580723064781\
49842952259558901275482397177332572291032576092979073329954505638836\
26404746502450808094691160726320874941439730007041114185955302788273\
57654819182002449697761111346318195282761590964189790958117338627206\
08891043294524497853514701411244214305548608963957837834732532359576\
32914389252883939862562732428627755631404638303891684216331134456363\
09571965978466338551492316196335675355138403425804162919837822266909\
52177015317533873028461084188655413832917195133211789572854166208482\
36828179325129312375215419269702697032994776438233864830088715303734\
05666383868294088487730721762268849023084934661194260180272613802108\
00507821574100605484820134785957810277070778065551277254050167433239\
60662532164150048087724030476119290322101543853531386855384864255707\
90795341176519571188683739880683895792743749683498142923292196309777\
09014393684365533335930782018131299345502420604456334057860696247196\
15056033948995233218004343599672566239271964354028720554750120798543\
31970674797313126813523653744085662263206768837585132782896252333284\
34181297762469707954343600349234315923967476363891211528540665778364\
62139112474470512552263427012395270181270454916480459322481088586746\
00952306793175967755581011679940005249806303763141344412269037034987\
35579991600925924807505248554156826628176081544630830540667741263012\
44418642041083731190931300011544705602777737243780671888997708510567\
27276781247198832857695844217588895160467868204810010047816462358220\
83853248813427083407986848663216272020882330872781908537884546913155\
60217288731219073939652092602291014775270809308653649798585540105774\
50279289814603688431821508637246216967872282169347370599286277112447\
69092090298832016683017027342025976567170986331121634950217126442682\
71196502640542282317596308744753018471940955242634114984695080733900\
80000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000

Not sure how far it can go. Need to try sometime, but this looks amazing calculations by the cpu.

Thank you for sharing, rveri. I took the liberty of further distilling your contribution. The following use a subset of the tools used by your originals.

Distilled ascending:
n=20; for i in $(seq $n); do s=$(seq -s\* $i); printf "\"$s= \"\n$s\n" | bc; done

Distilled descending:
n=20; for i in $(seq $n -1 1); do s=$(seq -s\* $i); printf "\"$s= \"\n$s\n" | bc; done

Regards,
Alister

1 Like

Alister,
Thanks for the additional code it works fine, the seq -s is a great addition to it,

Further it is tried made as little as after shortened with the factorial notation:

Ascending: 
n=20;for i in $(seq $n);do printf $i\!=;seq -s\* $i|bc;done

Descending:
n=20;for i in $(seq $n -1 1);do printf $i\!=;seq -s\* $i|bc;done
# n=20;for i in $(seq $n);do printf $i\!=;seq -s\* $i|bc;done
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
11!=39916800
12!=479001600
13!=6227020800
14!=87178291200
15!=1307674368000
16!=20922789888000
17!=355687428096000
18!=6402373705728000
19!=121645100408832000
20!=2432902008176640000
# n=20;for i in $(seq $n -1 1);do printf $i\!=;seq -s\* $i|bc;done
20!=2432902008176640000
19!=121645100408832000
18!=6402373705728000
17!=355687428096000
16!=20922789888000
15!=1307674368000
14!=87178291200
13!=6227020800
12!=479001600
11!=39916800
10!=3628800
9!=362880
8!=40320
7!=5040
6!=720
5!=120
4!=24
3!=6
2!=2
1!=1
# 

Amazingly it works for factorial of 3000 also and so on....

n=3000;for i in $(seq $n);do printf $i\!=;seq -s\* $i|bc;done
3000!=41493596034378540855568670930866121709511191949318099176894676576975\
58565123531950086000765217800342007518463538361711849575087111404590\
77945534021610683396116210379041991775220626633901796828051647196974\
95968842457728766097103003726111095340241127118833157738815328438929\
73761302110631293037440148537872544607961029042949104979388812076251\
16251329170046416689621175902035751754889806535778689152850937824699...
.............................<135 line total>..............................................................
00000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000

It is amazing that it worked for 3000! also , but it used the top cpu it was 100% used, it was processing too fast.