例題
1, 2, 3, 4, 5, 6, 7 の中から4つの数字を使ってできる4桁の数の総和を求めなさい。
【解答】
4桁の数は、7×6×5×4=840個あります。
1, 2, 3, 4, 5, 6, 7 が千の位、百の位、十の位、一の位に現れるのは、それぞれ
6×5×4=120回なので
1×1000×840+1×100×840+1×10×840+1×1×840=1111×120
2×1000×840+2×100×840+2×10×840+2×1×840=2222×120
・・・
7×1000×840+7×100×840+7×10×840+7×1×840=7777×120
これらの和は
(1111+2222+3333+4444+5555+6666+7777)×120=3732960・・・(答)
下記に、この問題を解くpythonプログラムを示します。
順列はitertoolsモジュールの関数を使って求めることができます。
import itertools # 順列を生成するためのモジュール
num = {1, 2, 3, 4, 5, 6, 7}
A = set(itertools.permutations(num, 4)) # 順列の生成
list = []
for a in A:
list.append(1000*a[0]+100*a[1]+10*a[2]+a[3]) # 4桁の整数の生成
list.sort()
print(list)
print()
print('要素数:', len(A))
print('総和:', sum(list))
========== 出力結果 ==========
[1234, 1235, 1236, 1237, 1243, 1245, 1246, 1247, 1253, 1254, 1256, 1257, 1263, 1264, 1265, 1267, 1273, 1274, 1275, 1276, 1324, 1325, 1326, 1327, 1342, 1345, 1346, 1347, 1352, 1354, 1356, 1357, 1362, 1364, 1365, 1367, 1372, 1374, 1375, 1376, 1423, 1425, 1426, 1427, 1432, 1435, 1436, 1437, 1452, 1453, 1456, 1457, 1462, 1463, 1465, 1467, 1472, 1473, 1475, 1476, 1523, 1524, 1526, 1527, 1532, 1534, 1536, 1537, 1542, 1543, 1546, 1547, 1562, 1563, 1564, 1567, 1572, 1573, 1574, 1576, 1623, 1624, 1625, 1627, 1632, 1634, 1635, 1637, 1642, 1643, 1645, 1647, 1652, 1653, 1654, 1657, 1672, 1673, 1674, 1675, 1723, 1724, 1725, 1726, 1732, 1734, 1735, 1736, 1742, 1743, 1745, 1746, 1752, 1753, 1754, 1756, 1762, 1763, 1764, 1765, 2134, 2135, 2136, 2137, 2143, 2145, 2146, 2147, 2153, 2154, 2156, 2157, 2163, 2164, 2165, 2167, 2173, 2174, 2175, 2176, 2314, 2315, 2316, 2317, 2341, 2345, 2346, 2347, 2351, 2354, 2356, 2357, 2361, 2364, 2365, 2367, 2371, 2374, 2375, 2376, 2413, 2415, 2416, 2417, 2431, 2435, 2436, 2437, 2451, 2453, 2456, 2457, 2461, 2463, 2465, 2467, 2471, 2473, 2475, 2476, 2513, 2514, 2516, 2517, 2531, 2534, 2536, 2537, 2541, 2543, 2546, 2547, 2561, 2563, 2564, 2567, 2571, 2573, 2574, 2576, 2613, 2614, 2615, 2617, 2631, 2634, 2635, 2637, 2641, 2643, 2645, 2647, 2651, 2653, 2654, 2657, 2671, 2673, 2674, 2675, 2713, 2714, 2715, 2716, 2731, 2734, 2735, 2736, 2741, 2743, 2745, 2746, 2751, 2753, 2754, 2756, 2761, 2763, 2764, 2765, 3124, 3125, 3126, 3127, 3142, 3145, 3146, 3147, 3152, 3154, 3156, 3157, 3162, 3164, 3165, 3167, 3172, 3174, 3175, 3176, 3214, 3215, 3216, 3217, 3241, 3245, 3246, 3247, 3251, 3254, 3256, 3257, 3261, 3264, 3265, 3267, 3271, 3274, 3275, 3276, 3412, 3415, 3416, 3417, 3421, 3425, 3426, 3427, 3451, 3452, 3456, 3457, 3461, 3462, 3465, 3467, 3471, 3472, 3475, 3476, 3512, 3514, 3516, 3517, 3521, 3524, 3526, 3527, 3541, 3542, 3546, 3547, 3561, 3562, 3564, 3567, 3571, 3572, 3574, 3576, 3612, 3614, 3615, 3617, 3621, 3624, 3625, 3627, 3641, 3642, 3645, 3647, 3651, 3652, 3654, 3657, 3671, 3672, 3674, 3675, 3712, 3714, 3715, 3716, 3721, 3724, 3725, 3726, 3741, 3742, 3745, 3746, 3751, 3752, 3754, 3756, 3761, 3762, 3764, 3765, 4123, 4125, 4126, 4127, 4132, 4135, 4136, 4137, 4152, 4153, 4156, 4157, 4162, 4163, 4165, 4167, 4172, 4173, 4175, 4176, 4213, 4215, 4216, 4217, 4231, 4235, 4236, 4237, 4251, 4253, 4256, 4257, 4261, 4263, 4265, 4267, 4271, 4273, 4275, 4276, 4312, 4315, 4316, 4317, 4321, 4325, 4326, 4327, 4351, 4352, 4356, 4357, 4361, 4362, 4365, 4367, 4371, 4372, 4375, 4376, 4512, 4513, 4516, 4517, 4521, 4523, 4526, 4527, 4531, 4532, 4536, 4537, 4561, 4562, 4563, 4567, 4571, 4572, 4573, 4576, 4612, 4613, 4615, 4617, 4621, 4623, 4625, 4627, 4631, 4632, 4635, 4637, 4651, 4652, 4653, 4657, 4671, 4672, 4673, 4675, 4712, 4713, 4715, 4716, 4721, 4723, 4725, 4726, 4731, 4732, 4735, 4736, 4751, 4752, 4753, 4756, 4761, 4762, 4763, 4765, 5123, 5124, 5126, 5127, 5132, 5134, 5136, 5137, 5142, 5143, 5146, 5147, 5162, 5163, 5164, 5167, 5172, 5173, 5174, 5176, 5213, 5214, 5216, 5217, 5231, 5234, 5236, 5237, 5241, 5243, 5246, 5247, 5261, 5263, 5264, 5267, 5271, 5273, 5274, 5276, 5312, 5314, 5316, 5317, 5321, 5324, 5326, 5327, 5341, 5342, 5346, 5347, 5361, 5362, 5364, 5367, 5371, 5372, 5374, 5376, 5412, 5413, 5416, 5417, 5421, 5423, 5426, 5427, 5431, 5432, 5436, 5437, 5461, 5462, 5463, 5467, 5471, 5472, 5473, 5476, 5612, 5613, 5614, 5617, 5621, 5623, 5624, 5627, 5631, 5632, 5634, 5637, 5641, 5642, 5643, 5647, 5671, 5672, 5673, 5674, 5712, 5713, 5714, 5716, 5721, 5723, 5724, 5726, 5731, 5732, 5734, 5736, 5741, 5742, 5743, 5746, 5761, 5762, 5763, 5764, 6123, 6124, 6125, 6127, 6132, 6134, 6135, 6137, 6142, 6143, 6145, 6147, 6152, 6153, 6154, 6157, 6172, 6173, 6174, 6175, 6213, 6214, 6215, 6217, 6231, 6234, 6235, 6237, 6241, 6243, 6245, 6247, 6251, 6253, 6254, 6257, 6271, 6273, 6274, 6275, 6312, 6314, 6315, 6317, 6321, 6324, 6325, 6327, 6341, 6342, 6345, 6347, 6351, 6352, 6354, 6357, 6371, 6372, 6374, 6375, 6412, 6413, 6415, 6417, 6421, 6423, 6425, 6427, 6431, 6432, 6435, 6437, 6451, 6452, 6453, 6457, 6471, 6472, 6473, 6475, 6512, 6513, 6514, 6517, 6521, 6523, 6524, 6527, 6531, 6532, 6534, 6537, 6541, 6542, 6543, 6547, 6571, 6572, 6573, 6574, 6712, 6713, 6714, 6715, 6721, 6723, 6724, 6725, 6731, 6732, 6734, 6735, 6741, 6742, 6743, 6745, 6751, 6752, 6753, 6754, 7123, 7124, 7125, 7126, 7132, 7134, 7135, 7136, 7142, 7143, 7145, 7146, 7152, 7153, 7154, 7156, 7162, 7163, 7164, 7165, 7213, 7214, 7215, 7216, 7231, 7234, 7235, 7236, 7241, 7243, 7245, 7246, 7251, 7253, 7254, 7256, 7261, 7263, 7264, 7265, 7312, 7314, 7315, 7316, 7321, 7324, 7325, 7326, 7341, 7342, 7345, 7346, 7351, 7352, 7354, 7356, 7361, 7362, 7364, 7365, 7412, 7413, 7415, 7416, 7421, 7423, 7425, 7426, 7431, 7432, 7435, 7436, 7451, 7452, 7453, 7456, 7461, 7462, 7463, 7465, 7512, 7513, 7514, 7516, 7521, 7523, 7524, 7526, 7531, 7532, 7534, 7536, 7541, 7542, 7543, 7546, 7561, 7562, 7563, 7564, 7612, 7613, 7614, 7615, 7621, 7623, 7624, 7625, 7631, 7632, 7634, 7635, 7641, 7642, 7643, 7645, 7651, 7652, 7653, 7654]
要素数: 840
総和: 3732960