When you divide the successive powers of 10
by 13
you get the following remainders of the integer divisions:
1, 10, 9, 12, 3, 4
.
Then the whole pattern repeats.
Hence the following method: Multiply the right most digit of the number with the left most number in the sequence shown above, the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary.
…………………………………………………………………
Example: What is the remainder when 1234567
is divided by 13
?
7×1 + 6×10 + 5×9 + 4×12 + 3×3 + 2×4 + 1×1 = 178
We repeat the process with 178:
8x1 + 7x10 + 1x9 = 87
and again with 87:
7x1 + 8x10 = 87
…………………………………………………………………
From now on the sequence is stationary and the remainder of 1234567
by 13
is the same as the remainder of 87
by 13
: 9
Call thirt
the function which processes this sequence of operations on an integer n (>=0)
. thirt
will return the stationary number.
thirt(1234567)
calculates 178, then 87, then 87 and returns 87
.
thirt(321)
calculates 48, 48 and returns 48
代码:
import java.util.ArrayList; import java.util.List; /** * @description: * @create: 2019/04/16 19:01 */ public class Thirteen { public static long thirt(long n) { //声明一个数组来存放sum值 List<Long> list = new ArrayList<>(); //第一次添加原始值 list.add(n); //做do-while循环,直到list中最后两个值相等,跳出循环,并返回结果 do { //迭代方法 long num = sum(list); list.add(num); } while (!((list.get(list.size() - 2)) == (list.get(list.size() - 1)))); return list.get(list.size() - 1); } //输入一个值,根据规则,计算他的和,得出新的数值 public static Long sum(List<Long> list) { if (list.size() > 0) { //得到上次计算结果的新数值,即list记录中的最后一个数字 Long n = list.get(list.size() - 1); //将数值转换为字符串 String str = String.valueOf(n); StringBuilder sb = new StringBuilder(str); //将字符串翻转,以便于计算 str = sb.reverse().toString(); int length = str.length(); //将10的连续幂除以13的余数值存入数组中 int[] arr = {1, 10, 9, 12, 3, 4}; long sum = 0; //遍历数值,取每一位,与余数进行相乘,取和得到新数值并返回 for (int i = 0; i < length; i++) { //根据数值的位数位置,计算出与之相乘的余数 int k = i % 6; int a = arr[k]; //取出相应的位数 String ch = String.valueOf(str.charAt(i)); Integer in = Integer.valueOf(ch); //位数与余数相乘,并求和 sum = sum + in * a; } //返回一个新数值 return sum; } //随意返回一个数值,这里随便写 return 0l; } //方法二,用递归 private static final int a[] = {1, 10, 9, 12, 3, 4}; //pattern public static long thirt2(long n) { // New number long r = 0; //Converting long to String String s = new String("" + n); for (int i = s.length() - 1; i >= 0; --i) r += (s.charAt(i) - '0') * a[(s.length() - i - 1) % 6]; if (r == n) return r; return thirt(r); } public static void main(String[] args) { System.out.println(thirt(321)); System.out.println(thirt2(321)); } }
今天的文章10的几次方除以10的几次方怎么算_一个数连续除以两个数可以怎么样「建议收藏」分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/88777.html