MEWBIES@: Facebook Twitter G+ YouTube DeviantArt Forum Wall
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
██ ██
█▌ - FUN ON THE TERMINAL PART 4 - █▌
█▌ MATH ON THE TERMINAL █▌
█ ▐▌
█ Even if math isn't your thing it's fascinating to see what's possible on ▐▌
█ your terminal and you might even learn some new command lines. ▐▌
█ ▐▌
█ NOTE: ▐▌
█ 1.) If you aren't familiar with command lines ending with: \ Read HERE. ▐▌
█ Do NOT add a space at the end of the \ or the cmd won't work. ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ CALCULATOR - BC ▐▌
█ MULTIPLICATION TABLE ▐▌
█ PRIME TIME - FACTOR ▐▌
█ GIMPS (FREEBSD, LINUX, MAC, WIN) ▐▌
█ PI SPEED TEST ▐▌
█ MAGIC SQUARE GENERATOR ▐▌
█ GENERATE RANDOM NUMBERS ▐▌
█ UNITS CONVERSION ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ CACULATOR - BC: ▐▌
█ ``````````````` ▐▌
█ bc - "An arbitrary precision calculator language". Sometimes referred to ▐▌
█ as bench calculator. To start just some quickie commands in case you ▐▌
█ didn't even know it was there and how easy it is to use it - ▐▌
█ ▐▌
█ Addition: ▐▌
█ echo '9+2' | bc ▐▌
█ Or you could enter it as: ▐▌
█ echo $((9+2)) ▐▌
█ Reply: 11 ▐▌
█ ▐▌
█ Subtraction: ▐▌
█ echo '9-2' | bc or echo $((9-2)) ▐▌
█ Reply: 7 ▐▌
█ ▐▌
█ Multiplication: ▐▌
█ echo '9*2' | bc or echo $((9*2)) ▐▌
█ Reply: 18 ▐▌
█ ▐▌
█ Division: ▐▌
█ echo '9/2' | bc or echo $((9/2)) ▐▌
█ Reply: 4 ▐▌
█ Hmm 4?! That is because by default bc is set to have zero digits after the ▐▌
█ decimal. ▐▌
█ We must instruct bc how many digits to carry on for. ▐▌
█ To do this we can use -l which its default is twenty digits: ▐▌
█ echo '9/2' | bc -l ▐▌
█ Reply: 4.50000000000000000000 ▐▌
█ ▐▌
█ Scale: ▐▌
█ Or we can use 'scale' and state exactly how many digits - ▐▌
█ ['scale']=[the number of digits after the decimal]; ▐▌
█ For example echo '9/2' | bc, 10 digits after the decimal: ▐▌
█ echo 'scale=10;9/2' | bc ▐▌
█ Reply: 4.5000000000 ▐▌
█ ▐▌
█ Power: ▐▌
█ (a × itself the number of times y states, so 2^9 (2 to the ninth power) ▐▌
█ would be: 2×2×2×2×2×2×2×2×2=512) ▐▌
█ echo '2^9' | bc ▐▌
█ Reply: 512 ▐▌
█ ▐▌
█ Square Root (y × y = a. a being any number, and we want to find out what y ▐▌
█ is. y = the square the root of a. For example the square root of 9 is 3 ▐▌
█ (3×3=9). ▐▌
█ What is the square root of 2?: ▐▌
█ echo 'sqrt(2)' | bc ▐▌
█ Reply: 1 ▐▌
█ We know 1 × 1 = 1 so that can't be, so lets add 'scale' and state to reply ▐▌
█ with a 1,000 digits after the decimal: ▐▌
█ echo 'scale=1000;sqrt(2)' | bc ▐▌
█ Reply: ▐▌
█ 1.414213562373095048801688724209698078569671875376948073176679737990\ ▐▌
█ 73247846210703885038753432764157273501384623091229702492483605585073\ ▐▌
█ 72126441214970999358314132226659275055927557999505011527820605714701\ ▐▌
█ [snip] ▐▌
█ ▐▌
█ Now you can see why it is better we 'state'. :) ▐▌
█ ▐▌
█ bcs' default output on the terminal is 70 before it breaks, \, into a new ▐▌
█ line. I'll show you a way how to change that default break amount further ▐▌
█ on. ▐▌
█ ▐▌
█ As you can see from the example above one of the great advantages of using ▐▌
█ bc on the terminal to calculate over standard calculators is the results ▐▌
█ can be thousands of digits long. On my Debian I did a calculation with the ▐▌
█ results having 5,000 digits (shown below). Also you are able to view the ▐▌
█ calculations you enter, can output the results to a file and input ▐▌
█ calculations from a file. Btw bc is often in scripting, etc also. ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ MULTIPLICATION TABLE: ▐▌
█ ````````````````````` ▐▌
█ I remember in grade school a student demonstrating to me 'evil' numbers ▐▌
█ (2, 3 & 7) on a calculator. He entered 2 × 3 × 3 × 3 × 3 × 7, then he ▐▌
█ turned the calculator upside down to show me the results. It's true! 1134 ▐▌
█ which upside down read as 'hell'. ▐▌
█ ▐▌
█ I came across HERE a nice cmd line to print out a multiplication table: ▐▌
█ for i in {1..9}; do for j in $(seq 1 $i); do echo -ne $i*$j=$((i*j))\\t;\ ▐▌
█ done; echo;done ▐▌
█ The reply is: ▐▌
█ 1×1=1 ▐▌
█ 2×1=2 2×2=4 ▐▌
█ 3×1=3 3×2=6 3×3=9 ▐▌
█ 4×1=4 4×2=8 4×3=12 4×4=16 ▐▌
█ 5×1=5 5×2=10 5×3=15 5×4=20 5×5=25 ▐▌
█ 6×1=6 6×2=12 6×3=18 6×4=24 6×5=30 6×6=36 ▐▌
█ 7×1=7 7×2=14 7×3=21 7×4=28 7×5=35 7×6=42 7×7=49 ▐▌
█ 8×1=8 8×2=16 8×3=24 8×4=32 8×5=40 8×6=48 8×7=56 8×8=64 ▐▌
█ 9×1=9 9×2=18 9×3=27 9×4=36 9×5=45 9×6=54 9×7=63 9×8=72 9×9=81 ▐▌
█ ▐▌
█ Or if you have boxes installed: ▐▌
█ for i in {1..9}; do for j in $(seq 1 $i); do echo -ne $i*$j=$((i*j))\\t;\ ▐▌
█ done; echo;done | boxes -d peek ▐▌
█ ▐▌
█ /* _\|/_ ▐▌
█ (o o) ▐▌
█ +----oOO-{_}-OOo-------------------------------------------------------+ ▐▌
█ |1*1=1 | ▐▌
█ |2*1=2 2*2=4 | ▐▌
█ |3*1=3 3*2=6 3*3=9 | ▐▌
█ |4*1=4 4*2=8 4*3=12 4*4=16 | ▐▌
█ |5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 | ▐▌
█ |6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 | ▐▌
█ |7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 | ▐▌
█ |8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 | ▐▌
█ |9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81| ▐▌
█ +---------------------------------------------------------------------*/ ▐▌
█ ▐▌
█ To increase table for example to 13, just change 1..9 to 1..13: ▐▌
█ for i in {1..13}; do for j in $(seq 1 $i); do echo -ne $i*$j=$((i*j))\\t;\ ▐▌
█ done; echo;done ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ PRIME TIME - FACTOR: ▐▌
█ ```````````````````` ▐▌
█ No need to install - Factor helps you to discover if a number is a ▐▌
█ 'prime number' and gives you the prime factors of a number. ▐▌
█ A prime number is one that is only divisible by the number 1 and itself. ▐▌
█ For example the number 3 is only divisible by 1 and 3. So 3 is a prime ▐▌
█ number. Whereas 4 is divisible by 1, 2, and 4. So the number 4 is not a ▐▌
█ prime number. To eliminate some possibilities of a number being a prime ▐▌
█ number - any number ending with a 0 isn't a prime, the only even number ▐▌
█ that is a prime is 2 and the only number ending with a 5 that is a prime ▐▌
█ number is 5. ▐▌
█ ▐▌
█ Two help cmds: ▐▌
█ man factor ▐▌
█ info coreutils 'factor invocation' ▐▌
█ ▐▌
█ Examples to try: ▐▌
█ factor 1 ▐▌
█ Lets factor a few numbers at a time: ▐▌
█ factor 2 && factor 3 && factor 4 && factor 11 && factor 13 && \ ▐▌
█ factor 14 && factor 20 && factor 127 && factor 1000 ▐▌
█ Reply: ▐▌
█ 2: 2 ▐▌
█ 3: 3 ▐▌
█ 4: 2 2 ▐▌
█ 11: 11 ▐▌
█ 13: 13 ▐▌
█ 14: 2 7 ▐▌
█ 20: 2 2 5 ▐▌
█ 127: 127 ▐▌
█ 1000: 2 2 2 5 5 5 ▐▌
█ If a number has more than one number in the reply, it isn't a prime number ▐▌
█ as 20 shows (2 × 2 × 5 = 20) ▐▌
█ But you might be wondering why it just didn't print out for 20: 2 10 (2 x ▐▌
█ 10 = 20) ▐▌
█ That is because 10 isn't a prime number. Notice that the output factor ▐▌
█ gives for every number is a prime number (2, 3, 5, 11, 13, 7, etc). ▐▌
█ :)) sweeet ▐▌
█ ▐▌
█ factor 104727 ▐▌
█ 104727: 3 7 4987 (not a prime number: 3 × 7 × 4987 = 104727) ▐▌
█ factor 104729 ▐▌
█ 104729: 104729 (prime number) ▐▌
█ Add 'time' to output the length of time taken to calculate: ▐▌
█ time factor 1337 ▐▌
█ Reply: ▐▌
█ 1337: 7 191 ▐▌
█ real 0m0.003s ▐▌
█ user 0m0.004s ▐▌
█ sys 0m0.000s ▐▌
█ ▐▌
█ Click HERE to view the first 10,000 prime numbers. ▐▌
█ ▐▌
█ And out comes the cow ▐▌
█ cowthink -f moofasa $(factor 1337 && factor 7191) ▐▌
█ _____________________________ ▐▌
█ ( 1337: 7 191 7191: 3 3 17 47 ) ▐▌
█ ----------------------------- ▐▌
█ o ____ ▐▌
█ o / \ ▐▌
█ | ^__^ | ▐▌
█ | (oo) |______ ▐▌
█ | (__) | )\/\ ▐▌
█ \____/|----w | ▐▌
█ || || ▐▌
█ ▐▌
█ Moofasa ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ GIMPS (FREEBSD, LINUX, MAC, WIN): ▐▌
█ ````````````````````````````````` ▐▌
█ Since we just played with prime numbers I thought I would mention 'GIMPS' ▐▌
█ 'Great Internet Mersenne Prime Search'. A large internet community working ▐▌
█ together (distributed computing) to discover the next largest ▐▌
█ 'Mersenne prime' by each person running a tiny portable program in the ▐▌
█ background of their PC, each working on a piece of the greater puzzle. ▐▌
█ You don't need to know a single thing about prime numbers to join in :). ▐▌
█ GIMPS site is HERE. It is 100% free, open source and you can be ▐▌
█ completely anonymous if you like. You can allocate how much CPU usage you ▐▌
█ want the program to use and when. I made a mini GIMPS tutorial if you need ▐▌
█ help to install it, under GEEK FUN on the left menu, or click HERE. ▐▌
█ ▐▌
█ Join Team Mewbies: ▐▌
█ Update: I just created team mewbies account on GIMPS. Maybe 1 of the few ▐▌
█ people that might read this has a shell that they can spare some CPU ▐▌
█ cycles and join team mewbies :) . You can even create your own team. To ▐▌
█ join a team though you will need to sign up. You'll have your own private ▐▌
█ account, then you can join or leave a team with a push of a button; click ▐▌
█ HERE after you login to view the team page, search for mewbies, then press ▐▌
█ the button 'Join'. No one, including other team members can view your ▐▌
█ private information. ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ PI SPEED TEST: ▐▌
█ `````````````` ▐▌
█ I found a nice cmd line using 'bc' to print out pi (3.14 ... ratio of the ▐▌
█ circumference of a circle to the diameter) with as many digits after the ▐▌
█ decimal that you state from basicallytech.com HERE (and lots of other bc ▐▌
█ examples). Just change the 'scale' variable - ▐▌
█ ▐▌
█ This prints out 10: ▐▌
█ echo "scale=10; 4*a(1)" | bc -l ▐▌
█ 3.1415926532 ▐▌
█ This prints out 100: ▐▌
█ echo "scale=100; 4*a(1)" | bc -l ▐▌
█ ▐▌
█ On bacicallytech.com, same article, he has started a little fun ▐▌
█ comparative speed test of your PC using bc to calculate pi. ▐▌
█ First view some CPU specs to compare with others: ▐▌
█ cat /proc/cpuinfo | egrep "model name|MHz" ▐▌
█ My reply: ▐▌
█ model name : AMD Sempron(tm) Processor 2600+ ▐▌
█ cpu MHz : 1599.842 ▐▌
█ Some of other details, why not: ▐▌
█ Debian GNU/Linux 6.0 i686 2.6.26-2-486 ▐▌
█ Uptime 261days 23hours 49minutes 8seconds ▐▌
█ ▐▌
█ Then run the pi test: ▐▌
█ time echo "scale=5000; 4*a(1)" | bc -l -q ▐▌
█ My reply: ▐▌
█ 3.14[snip]73774418426312986080998886874132604720 ▐▌
█ real 1m9.159s ▐▌
█ user 1m6.792s ▐▌
█ sys 0m0.068s ▐▌
█ ▐▌
█ To lengthen a line's result before it breaks with a '/' at the 70th ▐▌
█ column, (per session) to 105 for example, Rob had posted how on the same ▐▌
█ page: ▐▌
█ ▐▌
█ export BC_LINE_LENGTH=105 ▐▌
█ Test again: ▐▌
█ cowthink -f three-eyes me want fresh pi $(echo "scale=100; 4*a(1)" | bc -l)▐▌
█ _________________________________________ ▐▌
█ ( me want fresh pi ) ▐▌
█ ( 3.1415926535897932384626433832795028841 ) ▐▌
█ ( 971693993751058209749445923078164062862 ) ▐▌
█ ( 089986280348253421170676 ) ▐▌
█ ----------------------------------------- ▐▌
█ o ^___^ ▐▌
█ o (ooo)\_______ ▐▌
█ (___)\ )\/\ ▐▌
█ ||----w | ▐▌
█ || || ▐▌
█ ▐▌
█ To reset the line length back to its default (it will auto reset upon new ▐▌
█ login): ▐▌
█ export BC_LINE_LENGTH=70 ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ MAGIC SQUARE GENERATOR: ▐▌
█ ``````````````````````` ▐▌
█ "Magic square is a two-dimensional array of integers in which all the ▐▌
█ rows, columns, and *long* diagonals add up to the same number. Being ▐▌
█ "square," the array has the same number of rows and columns. An example ▐▌
█ of a magic square of order 3 is: ▐▌
█ 8 1 6 ▐▌
█ 3 5 7 ▐▌
█ 4 9 2 ▐▌
█ All the rows, columns, and the two long diagonals add up to 15." ▐▌
█ ▐▌
█ This script is from ABS Guide - "Advanced Bash Scripting Guide' HERE. ▐▌
█ ▐▌
█ I love it, so clever. Copy / paste in the script yourself or: ▐▌
█ wget http://mewbies.com/geek_fun_files/numbers/msquare.zip ▐▌
█ unzip msquare.zip && chmod msquare.sh 755 && ./msquare.sh ▐▌
█ ▐▌
█ ./msquare.sh 5 ▐▌
█ 17 24 1 8 15 ▐▌
█ 23 5 7 14 16 ▐▌
█ 4 6 13 20 22 ▐▌
█ 10 12 19 21 3 ▐▌
█ 11 18 25 2 9 ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ GENERATE RANDOM NUMBERS: ▐▌
█ ```````````````````````` ▐▌
█ Produce a random integer between 0 and 32767. Comes with Debian. ▐▌
█ Examples: ▐▌
█ echo $RANDOM ▐▌
█ Produce nine random numbers: ▐▌
█ for i in {1..9}; do echo $RANDOM; done ▐▌
█ Display a random integer between 1 - 10000 ▐▌
█ echo $((RANDOM%10000+1)) ▐▌
█ Many other uses for random: ▐▌
█ man random ▐▌
█ man 3 random ▐▌
█ ▐▌
█ ___________..___________ ▐▌
█ ▐▌
█ UNITS CONVERSION: ▐▌
█ ````````````````` ▐▌
█ "GNU 'units' program converts quantities expressed in various scales to ▐▌
█ their equivalents in other scales. Debian package information. ▐▌
█ http://packages.debian.org/squeeze/units ▐▌
█ ▐▌
█ su ▐▌
█ aptitude install units ▐▌
█ exit ▐▌
█ For many working example: man units ▐▌
█ ▐▌
█ Here are some interactive examples: ▐▌
█ units ▐▌
█ You have: 10 meters ▐▌
█ You want: inches ▐▌
█ Reply: ▐▌
█ * 393.70079 ▐▌
█ / 0.00254 ▐▌
█ The answer will be marked with: * ▐▌
█ / is the inverse of the conversion you asked for ▐▌
█ ▐▌
█ You have: tempF (96) ▐▌
█ You want: tempC ▐▌
█ Reply: ▐▌
█ 35.555556 ▐▌
█ Ctrl+c ▐▌
█ ▐▌
█ Non-interactive examples: ▐▌
█ units '1 + 2' ▐▌
█ units '2 liters' 'quarts' ▐▌
█ units -t 'tempC(35)' 'tempF' ▐▌
█ units '(1/2) kg / (kg/meter)' ▐▌
█ units '(1/2) kg / (kg/meter)' 'league' ▐▌
█ indicate division of numbers with the vertical dash: | ▐▌
█ units '1|2 inch' 'mm' ▐▌
█ ▐▌
█ //---------------------------------------------------------------------- ▐▌
█ ▐▌
█ If you find mistakes, have suggestions, and or questions please post at ▐▌
█ mewbies forum HERE - thank you. ▐▌
█ ▐▌
█ Last update on 26 Apr '13 ▐▌
█ ▐▌
█▌ █▌
█▌ - mewbies.com - █▌
█▌ █▌
██▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄██