In my code I have 9 variables: numbers from 0 to 9. How can I tell the computer that all variables are different? Currently, I have this, but it doesn’t work: a !== b !== c !== d !== e !== f !== g !== h !== i (it’s a statement in an if statement). Why isn’t it working and how can I fix that?
You will have to post your full code for us to understand fully what the problem is.
My full code:
for (var a = 1; a < 10; a++) {
for (var b = 0; b < 10; b += 2) {
for (var c = 0; c < 10; c++) {
for (var d = 0; d < 10; d += 2) {
for (var e = 0; e < 10; e += 5) {
for (var f = 0; f < 10; f += 2) {
for (var g = 0; g < 10; g++) {
for (var h = 0; h < 10; h +=2) {
for (var i = 0; i < 10; i++) {
if(a !== b !== c !== d !== e !== f !== g !== h !== i && (a + b + c) % 3 === 0 && (10 * c + d) % 4 === 0 && (d + e + f) % 3 === 0 && (1000000 * a + 100000 * b + 10000 * c + 1000 * d + 100 * e + 10 * f + g) % 7 === 0 && (100 * f + 10 * g + h) % 8 === 0 && (a + b + c + d + e + f + g + h + i) % 9 === 0) {
console.log(a, b, c, d, e, f, g, h, i)
}
}}}}}}}}}
Try this:
if (a !== b || b !== c || c !== d || d !== e || e !== f || f !== g || g !== h || h !== i) {
But I want them all to be different from all.
It will be way too long to do it by just checking all combinations of variables. A better way is to just store what values have been used in a boolean array, and then check how many values where checked of. If exactly 9 values have been checked off, they’re unique.
Dammit people, let me answer first.
No!!!
Oh, well.
Thanks for your help, guys! It works now, like this:
for (var a = 1; a < 10; a++) {
for (var b = 0; b < 10; b += 2) {
for (var c = 0; c < 10; c++) {
for (var d = 0; d < 10; d += 2) {
for (var e = 0; e < 10; e += 5) {
for (var f = 0; f < 10; f += 2) {
for (var g = 0; g < 10; g++) {
for (var h = 0; h < 10; h +=2) {
for (var i = 0; i < 10; i++) {
if(a != b && a != c && a != d && a != e && a != f && a != g && a != h && a != i && b != c && b != d && b != e && b != f && b != g && b != h && b != i && c != d && c != e && c != f && c != g && c != h && c != i && d != e && d != f && d != g && d != h && d != i && e != f && e != g && e != h && e != i && f != g && f != h && f != i && g != h && g != i && h != i && (a + b + c) % 3 === 0 && (10 * c + d) % 4 === 0 && (d + e + f) % 3 === 0 && (1000000 * a + 100000 * b + 10000 * c + 1000 * d + 100 * e + 10 * f + g) % 7 === 0 && (100 * f + 10 * g + h) % 8 === 0 && (a + b + c + d + e + f + g + h + i) % 9 === 0) {
console.log(a, b, c, d, e, f, g, h, i)
}
}}}}}}}}}
What was it for, though?
It was a task on a math competition: Find the number(s) which have 9 digits which are all different and the number made from the first 2 digits is even, the number made from the first 3 digits can be divided by 3, the number made from the first 4 digits can be divided by 4, etc.