README: Factorial quick chart with sed & bc:


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting README: Factorial quick chart with sed & bc:
# 1  
Old 02-06-2013
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,



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
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:
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


Enjoy!. Have fun!.

Last edited by rveri; 02-06-2013 at 03:13 AM..
These 2 Users Gave Thanks to rveri For This Post:
# 2  
Old 02-06-2013
Nice example.
Buts its some more easy to read when using $(code) instead of `code`
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
This User Gave Thanks to Jotne For This Post:
# 3  
Old 02-06-2013
Inspirational, here is an awk version of the top half:
Code:
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}}'


Last edited by Scrutinizer; 02-06-2013 at 03:59 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-06-2013
awk does not work when n>170, not sure how long bc goes, but n=1000 works fine Smilie
This User Gave Thanks to Jotne For This Post:
# 5  
Old 02-06-2013
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:
Code:
168*169*170= 725741561530799404539963...48167424" [ length=307 column long.]

171! Gives: infinity.
Code:
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:
Code:
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
Code:
GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)

Code:
<.......>

represents the range of the display:



Code:
2000! =

Code:
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.
# 6  
Old 02-06-2013
Quote:
Originally Posted by rveri
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
...<snip>...
# 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
#

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.
Code:
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
This User Gave Thanks to alister For This Post:
# 7  
Old 02-07-2013
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:
Code:
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




Code:
# 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....
Code:
n=3000;for i in $(seq $n);do printf $i\!=;seq -s\* $i|bc;done

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

2. Shell Programming and Scripting

Quick Sed Question

Just want to know why when I do the following in sed, the required is not extracted. echo "ab01cde234" | sed 's/*$//' result: ab01cde (Which is correct) echo "ab01cde234" |sed 's/.*\(*\)$/\1/' result: blank (was expecting 234) or echo "ab01cde234" |sed 's/.*\(\)*$/\1/' result: blank... (6 Replies)
Discussion started by: eo29
6 Replies

3. Shell Programming and Scripting

Factorial of any number using functions

how to write the code for factorial of any number using functions and arguments????? (7 Replies)
Discussion started by: kullu
7 Replies

4. Programming

Quick question about '_&'

I've seen in other programmers code the use of '_&' as a line separator. I am trying to find in my C++ reference manual some pages dedicated to an explanation of the use of this '_&' but I don't know what it is called. I only know it is a "line separator" or "line break" of some sort which is... (0 Replies)
Discussion started by: sepoto
0 Replies

5. AIX

README: Install tripwire on AIX

We had a SAS70 audit at our site a few months back and part of the end result was that I had to install tripwire to monitor our application binaries. We were able to compile tripwire for our AIX 5.3 server but after a few months I was asked to install it on other systems. I could have and did just... (1 Reply)
Discussion started by: juredd1
1 Replies

6. Shell Programming and Scripting

sed & areas respectively sed & pyramiding

Hello everyone, i wonder if someone could give me an advice regarding the following problem using sed. Given ist a structure as shown below: <aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa> Now I want to change the outer tag from "aaa" to "new" and replace all tags inside the outer tags... (4 Replies)
Discussion started by: Donaldinho
4 Replies

7. Shell Programming and Scripting

I can't figure this out? Quick help with sed

I am trying to delete everything in the parenthesis(including the parenthesis) in this text: Wind: from the WNW (290 degrees) at 6 MPH (5 KT) Pressure (altimeter): 29.82 in. Hg (1009 hPa) Temperature: 80.1 F (26.7 C) Dew Point: 72.0 F (22.2 C) Relative Humidity: 76% Trying to make it look... (3 Replies)
Discussion started by: pmoore4321
3 Replies

8. Shell Programming and Scripting

quick sed help

what's the code for delete everything before sssss asdf become: (3 Replies)
Discussion started by: katrvu
3 Replies

9. Shell Programming and Scripting

quick sed question

hey, Im just wondering is there away to get sed to read from a variable eg it doesn't seem to work, i really need to be able to recursively change the same data set... (2 Replies)
Discussion started by: vbm
2 Replies

10. Shell Programming and Scripting

a quick SED question

I have a line EXTDIR=`echo $i | sed 's/\-tar.gz//'` which looks for files ending in -tar.gz, i would like to increase the functionality so that it looks for .tar.gz files as well as -tar.gz. Do i put the - in square brackets with a dot ? like this EXTDIR=`echo $i | sed 's/\tar.gz//'` ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies
Login or Register to Ask a Question