#Yours truly has invited five of his colleagues to share a Christmas lunch #with him around the circular dinner table in his kitchen. Unfortunately, #two of the colleagues are not on speaking terms with each other, so they #cannot be seated together. In how many ways can we be seated around the table? perms = [] keep = [] reject = [] #Circular Permutations = (N - 1)! #perms = 12345.to_s.chars.to_a.permutation.map(&:join) #Using alternate method perms = 123456.to_s.chars.to_a.permutation.map(&:join) for i in perms if i.to_s =~ /12/ then reject.push(i.to_s); next; end if i.to_s =~ /21/ then reject.push(i.to_s); next; end #alternate method needs following two lines if i.to_s =~ /^1.*2$/ then reject.push(i.to_s); next; end if i.to_s =~ /^2.*1$/ then reject.push(i.to_s); next; end keep.push(i.to_s) end puts "Total perms: #{perms.uniq.count}" puts "Keep: #{keep.uniq.count}" puts "Reject: #{reject.uniq.count}" #Output for first method: #Total perms: 120 #Keep: 72 #Reject: 48 puts keep.uniq.inspect puts keep.uniq.size puts puts puts reject.uniq.inspect #alternate method keep2 = [] for i in keep if i.to_s =~ /^6/ then keep2.push(i.to_s); next; end end puts "keep2.count: #{keep2.count}" #Output: 72 puts keep2.inspect