[英]Algorithm involving figuring out 10 digit number puzzle
To determine if a number is divisible by 7, take the last digit off the number, double it and subtract the doubled number from the remaining number. 要确定数字是否可以被7整除,请从数字中减去最后一位数字,将其加倍,然后从剩余数字中减去加倍的数字。 If the result is evenly divisible by 7 (eg 14, 7, 0, -7, etc.), then the number is divisible by seven. 如果结果可被7整除(例如14、7、0,-7等),则数字可被7整除。 This may need to be repeated several times. 这可能需要重复几次。 Example: Is 3101 evenly divisible by 7? 示例:3101被7整除吗?
310 - take off the last digit of the number which was 1
-2 - double the removed digit and subtract it
308 - repeat the process by taking off the 8
-16 - and doubling it to get 16 which is subtracted
14 - the result is 14 which is a multiple of 7
The following is the code that I did to get the number: 以下是我用来获取号码的代码:
for(int O =0; O <= 9 ; O++) {
String a = String.valueOf(number[0]);
String b = String.valueOf(number[1]);
String c = String.valueOf(number[2]);
String d = String.valueOf(number[3]);
String e = String.valueOf(number[4]);
String f = String.valueOf(number[5]);
String h = a+b+c+d+e+f;
int abcdef = Integer.valueOf(h);
if ( (abcdef -(2*O) % 7) ==0 )
number [6] = O;
}
However, it is not giving me a number of such kind.I was able to get a number up until 6 where up until each digit the number is divisible by the respective index(if I start with 1, not 0 for the index).Which means index 1 is divisible by 1, index 2 is divisible by 2, index 3 is divisible by 3,......index 7 is divisible by 7.I want to form a number of such kind.Note that I could have done it without using the algorithm by the following way: 但是,它并没有给我这样的数字,我能够得到一个直到6的数字,直到每个数字都可以被相应的索引整除(如果我以1,而不是0开头)。这意味着索引1可以被1整除,索引2可以被2整除,索引3可以被3整除,......索引7可以被7整除。我想形成这样的数字。请注意,我可以通过以下方式无需使用算法即可完成此操作:
for(int O =0; O <= 9 ; O++) {
String a = String.valueOf(number[0]);
String b = String.valueOf(number[1]);
String c = String.valueOf(number[2]);
String d = String.valueOf(number[3]);
String e = String.valueOf(number[4]);
String f = String.valueOf(number[5]);
String g = String.valueOf(O);
String h = a+b+c+d+e+f+g;
int abcdefg = Integer.valueOf(h);
if ( (abcdefg % 7) ==0 )
number [6] = O;
}
However, I really want to do it using the algorithm that I described in the beginning. 但是,我真的很想使用我在开始时描述的算法。
Example calling code : 调用代码示例 :
int [] num = new int [7];
for (int i = 1000000; i < 9999999; i++)
{
// put i into the array and check it
if (checkDigitsDivisible(i, num))
{
System.out.println(i);
}
}
Check if a number has the first digit evenly divisible by 1, first 2 digits evenly divisible by 2, first 3 digits divisible by 3, etc. 检查一个数字的前几位数是否可以被1整除,前两位数可以被2除数,前3位可以被3除数,依此类推。
public static boolean checkDigitsDivisible (int num, int [] arr)
{
int one = num / 1000000;
int two = num / 100000;
int three = num / 10000;
int four = num / 1000;
int five = num / 100;
int six = num / 10;
int seven = num;
arr[0] = one % 10;
arr[1] = two % 10;
arr[2] = three % 10;
arr[3] = four % 10;
arr[4] = five % 10;
arr[5] = six % 10;
arr[6] = seven % 10;
return (one % 1 == 0) &&
(two % 2 == 0) &&
(three % 3 == 0) &&
(four % 4 == 0) &&
(five % 5 == 0) &&
(six % 6 == 0) &&
(isDivisibleBy7(seven));
}
A recursive solution to check if a number is divisible by 7 (algorithm as described in the question): 检查数字是否可被7整除的递归解决方案(问题中所述的算法):
public static boolean isDivisibleBy7 (int n)
{
n = Math.abs(n);
if (n == 0 || n == 7 || n == 14)
{
return true;
}
else
{
int lastDigit = n % 10;
int lastDigitOff = n / 10;
int remainder = lastDigitOff - (lastDigit * 2);
remainder = Math.abs(remainder);
if (remainder > n)
{
return false;
}
return isDivisibleBy7(remainder);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.