Squares on a chessboard calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Squares on a chessboard calculation
# 1  
Old 05-14-2012
Squares on a chessboard calculation

Hi All,

Just curious if the following formula is possible within a shell script:

n x (n + 1) x (2n + 1)
______________________
6

so far im just using a simple expression but need to implement the above.

Many thanks in advance

Code:
 
#!/bin/sh
echo "\n"
 
echo -------- Squares on a chessboard Calculator -------"\n"
echo "Simply, this calculator will return the number of
squares on a chessboard, according to the size of
the square chosen\n"
 
echo "Please enter a square size (1-8)...\n"
read SQUARE_SIZE
 
NO_SQUARES=`expr $SQUARE_SIZE + 5`
echo $NO_SQUARES
exit 0

# 2  
Old 05-14-2012
Try, for e.g.

Code:
$ for n in 1 2 3; do
> echo $(( n * (n + 1) * ( 2*n + 1)))
> done
6
30
84
$

Code:
NO_SQUARES=$(( SQUARE_SIZE * (SQUARE_SIZE + 1) * ( 2*SQUARE_SIZE + 1)))

This User Gave Thanks to clx For This Post:
# 3  
Old 05-14-2012
Code:
[user@host ~]# cat test.sh
#!/bin/bash
n=$1
echo $((((n * (n + 1) * ((2 * n) + 1)) / 6)))
[user@host ~]#
[user@host ~]# ./test.sh 5
55
[user@host ~]# ./test.sh 6
91
[user@host ~]# ./test.sh 8
204
[user@host ~]#

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 05-14-2012
thanks both!

can I just ask though, what about dividing by 6, after the first calculation is complete?
# 5  
Old 05-14-2012
I didn't notice the divide by 6 thing. sorry for that.
Use balajesuri's solution.
# 6  
Old 05-14-2012
no problem, thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Squares in saved code

can you help i am merging 2 files together and saving to a third file with awk and its working with this code awk 'OFS="";NR==FNR{a=$0;next} {print a,"\n","\b",$0}' file1 file2 > file3the problem is in file3 when its saved i get a small square at the start of every 2nd line (see picture) ... (6 Replies)
Discussion started by: bob123
6 Replies

2. Shell Programming and Scripting

Calculation

Hi, i have a large file like this: Contig1 1 5 Contig1 2 4 Contig1 3 3 Contig1 4 5 Contig1 5 3 Contig1 6 4 Contig2 1 3 Contig2 2 7 Contig2 3 2 Contig2 4 9 Contig2 5 10 Contig2 6 3 Contig2 7 7 Contig2 8 2 Contig2 9 7 Contig2 10 5 contig1 2 4 contig1 3 3 contig1 4 5 (3 Replies)
Discussion started by: the_simpsons
3 Replies

3. Shell Programming and Scripting

Size calculation MB to GB

pcmpath query device |awk 'BEGIN{print "TYPE\tDEVICE NAME\tSERIAL\tSIZE\tHOSTNAME"} /DEVICE/ { disk=$5 printf "%s\t", $7 printf "%s\t", disk getline; printf "%s\t", substr($2, length($2)-3) ("bootinfo -s " disk) | getline; printf... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

4. Shell Programming and Scripting

VG calculation in GB

for i in `lsvg` do echo "VG Name:" $i echo "Total VG Size:" lsvg $i |grep "TOTAL PPs:" |awk '{print $7}' | cut -c2- echo "Free VG Size:" lsvg $i |grep "FREE PPs:" | awk '{print $7}' | cut -c2- done The PP Sizes are in MB. I like to have the sizes in GB. Also, I like to have the... (14 Replies)
Discussion started by: Daniel Gate
14 Replies

5. Shell Programming and Scripting

calculation

Could someone till me what this calculation really means let foo=`date "+(1%H-106)*60+1%M-100"` bar=foo+1440 (4 Replies)
Discussion started by: freddie999
4 Replies

6. Shell Programming and Scripting

Birthday Calculation

Hi I have a simple question. Is there an easy way to read a date of birth from a file and calculate how old that person is based on today's date? And would I need the make sure the birthdates are enterered in a particular format? Thanks (10 Replies)
Discussion started by: mustaine85
10 Replies

7. Shell Programming and Scripting

summery calculation

Hi All I want to make summery for Date=245Duration=545 Date=245Duration=10 Date=245Duration=278 Date=246Duration=30 Date=246Duration=178 Date=246Duration=414 Date=247Duration=17 Date=247Duration=281 Date=247Duration=9 Date=248Duration=968 Date=248Duration=550 Date=248Duration=1011... (1 Reply)
Discussion started by: nalakaatslt
1 Replies

8. Shell Programming and Scripting

calculation

Hi, I am in ksh88 I am trying to get the result of the calculation using 3 variables: TOTAL CAPACITY and get the following error: $DB_CAPACITY=(( $DB_SIZE * 100 / $TOTAL )) ksh: syntax error: `((' unexpected I cannot figure out what am I doing wrong... Thanks for any help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

9. Shell Programming and Scripting

Bash squares and exponents

I'm trying to write a simple bash script and I need something like var=2^(5+i) where i is another variable. How would do I this in bash? (1 Reply)
Discussion started by: jeriryan87
1 Replies
Login or Register to Ask a Question