1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| --------------------------------------------------------------------------------
-- "Le compte est bon" ---------------------------------------------------------
-- Script pour l'interpréteur Lua 5.2 ------------------------------------------
-- Auteur de la version originale : Roland Y. ----------------------------------
-- http://roland.xtreemhost.com/le-compte-est-bon/ -----------------------------
-- Auteur de la présente version : Roland Chastain -----------------------------
--------------------------------------------------------------------------------
function RandomPick(t)
local i = math.random(#t)
local n = t[i]
table.remove(t, i)
return n
end
function MayDiv(n1, n2)
local a, b
if n1 > n2 then
a, b = n1, n2
else
a, b = n2, n1
end
return a % b == 0
end
function Operate(n1, n2, op)
local a, b
if n1 > n2 then
a, b = n1, n2
else
a, b = n2, n1
end
if op == "+" then
return a + b
elseif op == "-" then
return a - b
elseif op == "*" then
return a * b
elseif op == "/" then
return a / b
end
end
function Concat(s1, s2)
if s1 ~= "" then
s1 = s1 .. s2
end
return s1
end
local t1, t2, t3 = {}, {}, {}
t1 = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
25, 50, 75, 100
}
math.randomseed(os.time())
for i = 1, 6 do
t2[i] = RandomPick(t1)
t3[i] = t2[i]
end
local st2 = table.concat(t2, ", ")
local guess = math.random(100, 999)
local res
local best_res = 0
local ans = ""
local best_ans = ""
local num1, num2
local opset = {}
local op
local cur_step = 0
local old_step = guess
local max_iter = 2000000
local cur_iter = 0
local file = "compte.txt"
repeat
cur_iter = cur_iter + 1
if #t3 <= 1 then
for i = 1, 6 do
t3[i] = t2[i]
end
ans = ""
end
num1 = RandomPick(t3)
num2 = RandomPick(t3)
if num1 == num2 then
opset = {"+", "*", "/"}
elseif MayDiv(num1, num2) then
opset = {"+", "-", "*", "/"}
else
opset = {"+", "-", "*"}
end
op = opset[math.random(#opset)]
res = Operate(num1, num2, op)
table.insert(t3, res)
if op == "-" or op == "/" then
if num1 > num2 then
if op ~= "/" or num2 ~= 1 then
ans = Concat(ans, "\n") ..
num1 .. op ..
num2 ..
"=" ..
res
end
else
ans = Concat(ans, "\n") ..
num2 .. op ..
num1 ..
"=" ..
res
end
elseif op ~= "*" or num2 ~= 1 then
ans = Concat(ans, "\n") ..
num2 .. op ..
num1 ..
"=" ..
res
end
cur_step = math.abs(res - guess)
if cur_step < old_step then
best_res = res
old_step = cur_step
best_ans = ans
end
until (res == guess) or (cur_iter == max_iter)
io.output(file)
io.write("Nombres utilisables : " .. st2 .. "\n")
io.write("Nombre à trouver : " .. guess .. "\n")
if best_res ~= guess then
io.write("Meilleur résultat : " .. best_res .. "\n")
else
io.write("Le compte est bon !\n")
end
best_ans = string.gsub(
best_ans,
"[%+%-%*/=]+",
function(a)
return " " .. a .. " "
end
)
best_ans = string.gsub(
best_ans,
"%d+",
function(a)
return string.format("%3d", a)
end
)
io.write(string.rep("=", 5 * 3) .. "\n" .. best_ans .. "\n" .. string.rep("=", 5 * 3)) |
Partager