生成具有唯一数字的对称表[关闭](Generating symetrical table with unique numbers [closed])
我需要生成一个类似于这个的表:
18 21 16 40 19 30 18 39 34 58 37 48 21 37 61 40 51 16 56 35 46 40 59 70 19 49 30第一列和第一行中的数字是相同的数字序列,因此最终表格将是对称的。 我需要的是获得一个10x10的表,所有表都有唯一的数字,所以没有重复,因为我以后需要这些作为唯一标识符。 这更像是一个数学问题,而不是编程。
编辑(素数):
2 3 5 7 11 13 17 19 23 29 2 5 7 9 13 15 19 21 25 31 3 8 10 14 16 20 22 26 32 5 12 16 18 22 24 28 34 7 18 20 24 26 30 36 11 24 28 30 34 40 13 30 32 36 42 17 36 40 46 19 42 48 23 52 29I need to generate a table similar to this one:
18 21 16 40 19 30 18 39 34 58 37 48 21 37 61 40 51 16 56 35 46 40 59 70 19 49 30The numbers in the first column and row are the same sequence of numbers so finally the table would be symmetrical. What I need is to get a 10x10 table, all with unique numbers so nothing repeats, as I need these as unique identifiers later. This is more of a mathematical problem rather than programming.
Edit (primes):
2 3 5 7 11 13 17 19 23 29 2 5 7 9 13 15 19 21 25 31 3 8 10 14 16 20 22 26 32 5 12 16 18 22 24 28 34 7 18 20 24 26 30 36 11 24 28 30 34 40 13 30 32 36 42 17 36 40 46 19 42 48 23 52 29最满意答案
这是一个解决方案:使你的矢量幂为10。
> n=10 > v=10^(1:n) > v [1] 1e+01 1e+02 1e+03 1e+04 1e+05 1e+06 1e+07 1e+08 1e+09 1e+10 > m=outer(v,v,"+") > m[which(upper.tri(m))] [1] 110 1010 1100 10010 10100 11000 [7] 100010 100100 101000 110000 1000010 1000100 [13] 1001000 1010000 1100000 10000010 10000100 10001000 [19] 10010000 10100000 11000000 100000010 100000100 100001000 [25] 100010000 100100000 101000000 110000000 1000000010 1000000100 [31] 1000001000 1000010000 1000100000 1001000000 1010000000 1100000000 [37] 10000000010 10000000100 10000001000 10000010000 10000100000 10001000000 [43] 10010000000 10100000000 11000000000 > length(unique(m[which(upper.tri(m))]) [1] 45Here is a solution: make your vector powers of ten.
> n=10 > v=10^(1:n) > v [1] 1e+01 1e+02 1e+03 1e+04 1e+05 1e+06 1e+07 1e+08 1e+09 1e+10 > m=outer(v,v,"+") > m[which(upper.tri(m))] [1] 110 1010 1100 10010 10100 11000 [7] 100010 100100 101000 110000 1000010 1000100 [13] 1001000 1010000 1100000 10000010 10000100 10001000 [19] 10010000 10100000 11000000 100000010 100000100 100001000 [25] 100010000 100100000 101000000 110000000 1000000010 1000000100 [31] 1000001000 1000010000 1000100000 1001000000 1010000000 1100000000 [37] 10000000010 10000000100 10000001000 10000010000 10000100000 10001000000 [43] 10010000000 10100000000 11000000000 > length(unique(m[which(upper.tri(m))]) [1] 45更多推荐
发布评论