Combination of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combination of numbers
# 1  
Old 02-11-2011
Question Combination of numbers

Hello Group,

I have a file of data that contain
1
2
3
4
5

I request you help with a shell script for generate all posible combination of these numbers with the following output:

Example:
1 + 2 + 3 + 4 + 5 =
2 + 2 + 3 + 4 + 5 =
3 + 2 + 3 + 4 + 5 =

Thanks in advance.

Carlos
# 2  
Old 02-11-2011
Ruby(1.9.1+)
Code:
 $ ruby -e 'a=File.read("file").split; a.permutation(a.length).to_a.each {|x| puts x.join}'

This User Gave Thanks to kurumi For This Post:
# 3  
Old 02-12-2011
Hello kurumi / Forum,

Thanks for your prompt response. I have Cygwin and I was unable to execute the ruby command.
Do you have an "awk" script version that I can use instead of this script?

I really appreciate it your help

Carlos
# 4  
Old 02-13-2011
try:
Code:
paste -sd " " file | awk '{split($0,a," ")}END{
for(i=1;i<=NF;i++) {printf $i;for(j=2;j<=NF;j++) {printf "+"a[j]}print "="}
}'
1+2+3+4+5=
2+2+3+4+5=
3+2+3+4+5=
4+2+3+4+5=
5+2+3+4+5=

This User Gave Thanks to yinyuemi For This Post:
# 5  
Old 02-13-2011
If you mean permutations, rather than combinations, this can be done relatively easily using recursion in a shell that has local variables:
Code:
#!/bin/ksh
function perm {
  typeset p=$1 s=$2 i
  for i in "${NR[@]}"
  do
    if [ $p -eq 1 ]; then
      printf "%s%s=\n" "$s" "$i"
    else
      perm $((p-1)) "$s$i+"
    fi
  done
}

while read nr; do
  NR[$((i++))]=$nr
done < infile
perm ${#NR[@]}

The trouble is the exponential increase in the time required with increasing numbers

With awk:
Code:
awk 'function perm(p,s,     i) {
       for(i=1;i<=n;i++)
         if(p==1)
           printf "%s%s=\n",s,A[i]
         else
           perm(p-1,s A[i]"+")
     }
     {
       A[++n]=$1
     }
     END{
       perm(n)
     }' infile

So I think this kind of exercise is best left to a general programming language

Last edited by Scrutinizer; 02-13-2011 at 07:27 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-13-2011
Quote:
Example:
1 + 2 + 3 + 4 + 5 =
2 + 2 + 3 + 4 + 5 =
3 + 2 + 3 + 4 + 5 =
Inho this not an example of combinations or an example of permutations.
What is the process?
# 7  
Old 02-13-2011
My understand is, this is homework again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Kill -9 -1 combination

Good morning, In a Production environment ive seen this command that kills processes kill -9 -1 Because i am in a production environmet i can not execute this comamnd, so i would like to know what is the difference for the conventional kill -9 PID ? Thanks a lot (11 Replies)
Discussion started by: alexcol
11 Replies

2. Shell Programming and Scripting

Combination of 6 nos

Hi folks, I have a numbers from 1-100 and from these nos I have 30 numbers.. From this 30 nos, I have to generate a combination of 6 nos... this 30 numbers will range from 1-100... ( FYI: This is not a lottery game - just kidding) ... I am trying out this in a shell script.. any ideas ? (3 Replies)
Discussion started by: gsiva
3 Replies

3. Programming

6 digits combination

Is there any program that can create 6 digit numbers with: (DIGIT_1)+(DIGIT_2)+(DIGIT_3)+(DIGIT_4)+(DIGIT_5)+(DIGIT_6)=10 Any perl or C also can. Anyone can help me? Thank you (6 Replies)
Discussion started by: Tzeronone
6 Replies

4. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

5. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

6. Shell Programming and Scripting

Escape and combination

Hi I have 2 files like: file1 a 12 b 1 a 3 file2 a 9 c 0 a 8 and i would like to get a 12 a 9 a 3 a 8 i can do it with grep and paste with 3 lines. I tried to combine using: (3 Replies)
Discussion started by: Dedalus
3 Replies

7. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

8. UNIX for Dummies Questions & Answers

combination of two commands

I want to show a output like this Lee Ballancore PID TTY TIME CMD 31799 pts/3 00:00:00 vim 31866 pts/3 00:00:00 vim 2495 pts/7 00:00:00 vim 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps Alistairr Rutherford PID TTY TIME CMD 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps ... (3 Replies)
Discussion started by: nehaquick
3 Replies

9. UNIX for Dummies Questions & Answers

Combination Of commands

Hello All, I just wanted to know what are the different ways of using commands in combination. The most common one which i know is using pipes. Also grouping is also done like ( ls; date) where output of both the commands is displayed. Are there any other ways of combining various... (2 Replies)
Discussion started by: rahulrathod
2 Replies

10. Filesystems, Disks and Memory

Partition combination

Hi all I've got MacOSX server which is a UNIX based system. I've got 2 partiontion an I like to make just one partition on he disk without loosing any data on part1. Is there a way to do that kind of thing in UNIX or do I have to format everything and put up the system again? Thanx for reading... (3 Replies)
Discussion started by: gardarm
3 Replies
Login or Register to Ask a Question