nice prakash. Algorithm is definitely better than brute force. But here is brute force anyway, just go from 2 to square root of n ;P
I initially misread it and thought you were asking for ANY {c,d} set in which c,d are factors of n (but not necessarily c*d = n) . That wouldve been all of the factors (12) , choose 2, unless I am mistaken. 12 nCr 2 = 66. #!/usr/bin/ruby -w #how many unique sets {a,b} can be formed if a,b are factors of N and axb=N # N = 24*33 n=792 #factors = [2,2,2,3,3,11] #results res=['1x792'] 2.upto(Math.sqrt(n)) {|c| next unless (c&1).zero? || c%3==0 || c%11==0 res<<c.to_s+'x'+(n/c).to_s if n%c==0 } p res, res.size # output $ ./set_factors.rb #["1x792", "2x396", "3x264", "4x198", "6x132", "8x99", "9x88", "11x72", "12x66", "18x44", "22x36", "24x33"] #12 On Aug 23, 2:39 pm, Nikhil Gupta <nikgp...@iitr.ernet.in> wrote: > sorry I wrote them in different order: > if (a,b) and (b,a) are considered same then answer is 12 > and if they are considered different it is 24. > > -- > Nikhil Gupta > Indian Institute of Technology Roorkee, > Roorkee, Uttarakhand, > India , 247667 > Phone: +91 9634990161 > email: nikgp...@iitr.ernet.in > nikhilg.8...@gmail.com -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.