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. If the result is evenly divisible by 7 (eg 14, 7, 0, -7, etc.), then the number is divisible by seven. This may need to be repeated several times. Example: Is 3101 evenly divisible by 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:
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.
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):
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.