1 + 3 + 4 + 9 = 17
1 / 17 ≈ 0.05882
3 / 17 ≈ 0.17647
4 / 17 ≈ 0.23529
9 / 17 ≈ 0.52941
可以发现,每一个商的第三位都是大于等于5的数,四舍五入后会进位。
下面的程序可以生成符合这样条件的数据。
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
import java.util.stream.IntStream;
public class Calculator {
private enum COMPAREMOD {
DAYU, DENGYU, XIAOYU
}
public Calculator() {
// 第几位小数
int bdNum1 = 3;
// 大于等于小于 数字几
int bdNum2 = 4;
// 计算100次,任意三个100以内的数,共有多少个数满足条件,与合计值相除后,商的第3位小数大于4
// doHalfAdjustRandom(100, bdNum1, bdNum2, COMPAREMOD.DAYU);
// 计算3个10以内的数字,共有多少个数满足条件,与合计值相除后,商的第3位小数大于4。
int max = 9;
int count = 0;
for (int i = 1; i <= max; i++) {
for (int j = 1; j <= max; j++) {
for (int k = 1; k <= max; k++) {
if (doHalfAdjustRecycle2(bdNum1, bdNum2, COMPAREMOD.DAYU, i, j, k)) {
count += 1;
}
}
}
}
if (count != 0) {
System.out.println("恭喜你,找到了" + count + "个这样的数!");
} else {
System.out.println("很遗憾,没找到!");
}
count = 0;
// 计算4个10以内的数字,共有多少个数满足条件,与合计值相除后,商的第3位小数大于4。
for (int i = 1; i <= 32767; i++) {
if (doHalfAdjustRecycle2(bdNum1, bdNum2, COMPAREMOD.DAYU, i, i + 1, i + 2, i + 3)) {
count += 1;
}
}
if (count != 0) {
System.out.println("恭喜你,找到了" + count + "个这样的数!");
} else {
System.out.println("很遗憾,没找到!");
}
}
/**
* 通过targetRecycle次运算,任意三个随机数,计算它们分别与合计的值进行除算,得到的商中满足条件的组合。
*
* @param targetRecycle 随机循环次数
* @param compareNumIndex 第几位小数
* @param comparedNum 和哪个数字进行比较
* @param mode 比较是大于等于还是等于
* @return
*/
private boolean doHalfAdjustRandom(int targetRecycle, int compareNumIndex, int comparedNum, COMPAREMOD mode) {
return doHalfAdjustRecycleRandom(targetRecycle, 0, compareNumIndex, comparedNum, mode);
}
/**
* @param targetRecycle 随机循环次数
* @param currentRecycle 随机循环当前次数
* @param compareNumIndex 第几位小数
* @param comparedNum 和哪个数字进行比较
* @param mode 比较是大于等于还是等于
* @return
*/
private boolean doHalfAdjustRecycleRandom(int targetRecycle, int currentRecycle, int compareNumIndex,
int comparedNum, COMPAREMOD mode) {
BigDecimal zero = new BigDecimal(0);
BigDecimal num1 = null;
BigDecimal num2 = null;
BigDecimal num3 = null;
try {
Random r = new Random();
num1 = new BigDecimal(r.nextInt(100));
num2 = new BigDecimal(r.nextInt(100));
num3 = new BigDecimal(r.nextInt(100));
} catch (Exception e) {
e.printStackTrace();
}
if (num1 == zero || num2 == zero || num3 == zero) {
return false;
}
BigDecimal sum = num1.add(num2).add(num3);
BigDecimal result1 = num1.divide(sum, 5, RoundingMode.UP);
BigDecimal result2 = num2.divide(sum, 5, RoundingMode.UP);
BigDecimal result3 = num3.divide(sum, 5, RoundingMode.UP);
int locThreshold1 = Integer.parseInt(result1.toString().substring(compareNumIndex + 1, compareNumIndex + 2));
int locThreshold2 = Integer.parseInt(result2.toString().substring(compareNumIndex + 1, compareNumIndex + 2));
int locThreshold3 = Integer.parseInt(result3.toString().substring(compareNumIndex + 1, compareNumIndex + 2));
if (compare(comparedNum, mode, locThreshold1, locThreshold2, locThreshold3)) {
System.out.println(num1 + "+" + num2 + "+" + num3 + "=" + sum);
System.out.println(num1 + "/" + sum + "=" + result1.toString().substring(0, compareNumIndex + 2));
System.out.println(num2 + "/" + sum + "=" + result2.toString().substring(0, compareNumIndex + 2));
System.out.println(num3 + "/" + sum + "=" + result3.toString().substring(0, compareNumIndex + 2));
System.out.println("****************\n");
// return true;
}
if (targetRecycle == currentRecycle) {
System.out.println("计算结束");
return false;
} else {
return doHalfAdjustRecycleRandom(targetRecycle, currentRecycle + 1, compareNumIndex, comparedNum, mode);
}
}
/**
* 指定任意个数的数字,计算它们分别与合计的值进行除算,得到的商中满足条件的组合。
*
* @param targetRecycle 随机循环次数
* @param currentRecycle 随机循环当前次数
* @param compareNumIndex 第几位小数
* @param comparedNum 和哪个数字进行比较
* @param mode 比较是大于等于还是等于
* @return
*/
private boolean doHalfAdjustRecycle2(int compareNumIndex, int comparedNum, COMPAREMOD mode, int... numParamArray) {
// numParamArray的合计
BigDecimal sum = new BigDecimal(IntStream.of(numParamArray).sum());
// 商的集合
BigDecimal[] thres = new BigDecimal[numParamArray.length];
for (int i = 0; i < thres.length; i++) {
// 求每一个数除以合计的商,第compareNumIndex+3位小数四舍五入。
thres[i] = new BigDecimal(numParamArray[i]).divide(sum, compareNumIndex + 3, RoundingMode.FLOOR);
// 求第compareNumIndex位小数
int locThreshold = Integer
.parseInt(thres[i].toString().substring(compareNumIndex + 1, compareNumIndex + 2));
// 如果第compareNumIndex位的小数值不满足要求,则返回false,终止程序
if (!compare(comparedNum, mode, locThreshold)) {
return false;
}
}
for (int i = 0; i < numParamArray.length; i++) {
System.out.println(
numParamArray[i] + "/" + sum + "≈" + thres[i].toString().substring(0, compareNumIndex + 2));
}
System.out.println("****************\n");
return true;
}
/**
* num与comparedNum做比较
*
* @param comparedNum 正整数
* @param mode 比较模式:大于,等于,小于
* @param num 正整数
* @return
*/
private boolean compare(int comparedNum, COMPAREMOD mode, int... numParamArray) {
for (int i = 0; i < numParamArray.length; i++) {
int compareSrc = numParamArray[i];
if (mode == COMPAREMOD.DAYU) {
if (compareSrc <= comparedNum)
return false;
} else if (mode == COMPAREMOD.DENGYU) {
if (compareSrc != comparedNum)
return false;
} else {
if (compareSrc >= comparedNum)
return false;
}
}
return true;
}
public static void main(String[] args) {
Calculator c = new Calculator();
}
}
计算结果
1/6≈0.166
1/6≈0.166
4/6≈0.666
1/6≈0.166
4/6≈0.666
1/6≈0.166
1/13≈0.076
4/13≈0.307
8/13≈0.615
1/15≈0.066
7/15≈0.466
7/15≈0.466
1/13≈0.076
8/13≈0.615
4/13≈0.307
2/7≈0.285
2/7≈0.285
3/7≈0.428
2/12≈0.166
2/12≈0.166
8/12≈0.666
2/7≈0.285
3/7≈0.428
2/7≈0.285
2/12≈0.166
5/12≈0.416
5/12≈0.416
2/16≈0.125
7/16≈0.437
7/16≈0.437
2/12≈0.166
8/12≈0.666
2/12≈0.166
3/7≈0.428
2/7≈0.285
2/7≈0.285
3/16≈0.187
6/16≈0.375
7/16≈0.437
3/16≈0.187
7/16≈0.437
6/16≈0.375
4/6≈0.666
1/6≈0.166
1/6≈0.166
4/13≈0.307
1/13≈0.076
8/13≈0.615
4/14≈0.285
4/14≈0.285
6/14≈0.428
4/15≈0.266
4/15≈0.266
7/15≈0.466
4/17≈0.235
4/17≈0.235
9/17≈0.529
4/14≈0.285
5/14≈0.357
5/14≈0.357
4/14≈0.285
6/14≈0.428
4/14≈0.285
4/15≈0.266
7/15≈0.466
4/15≈0.266
4/13≈0.307
8/13≈0.615
1/13≈0.076
4/17≈0.235
9/17≈0.529
4/17≈0.235
5/12≈0.416
2/12≈0.166
5/12≈0.416
5/14≈0.357
4/14≈0.285
5/14≈0.357
5/12≈0.416
5/12≈0.416
2/12≈0.166
5/14≈0.357
5/14≈0.357
4/14≈0.285
6/16≈0.375
3/16≈0.187
7/16≈0.437
6/14≈0.428
4/14≈0.285
4/14≈0.285
6/19≈0.315
6/19≈0.315
7/19≈0.368
6/21≈0.285
6/21≈0.285
9/21≈0.428
6/16≈0.375
7/16≈0.437
3/16≈0.187
6/19≈0.315
7/19≈0.368
6/19≈0.315
6/21≈0.285
9/21≈0.428
6/21≈0.285
7/15≈0.466
1/15≈0.066
7/15≈0.466
7/16≈0.437
2/16≈0.125
7/16≈0.437
7/16≈0.437
3/16≈0.187
6/16≈0.375
7/15≈0.466
4/15≈0.266
4/15≈0.266
7/16≈0.437
6/16≈0.375
3/16≈0.187
7/19≈0.368
6/19≈0.315
6/19≈0.315
7/15≈0.466
7/15≈0.466
1/15≈0.066
7/16≈0.437
7/16≈0.437
2/16≈0.125
8/13≈0.615
1/13≈0.076
4/13≈0.307
8/12≈0.666
2/12≈0.166
2/12≈0.166
8/13≈0.615
4/13≈0.307
1/13≈0.076
8/26≈0.307
9/26≈0.346
9/26≈0.346
9/17≈0.529
4/17≈0.235
4/17≈0.235
9/21≈0.428
6/21≈0.285
6/21≈0.285
9/26≈0.346
8/26≈0.307
9/26≈0.346
9/26≈0.346
9/26≈0.346
8/26≈0.307
恭喜你,找到了51个这样的数!
很遗憾,没找到!
使用下面的程序,可以使用更大的数据去验证,是否存在连续的数字符合这样的计算方式。**
执行结果是没有找到,大概率是不存在的。
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
import java.util.stream.IntStream;
public class Calculator2 {
private enum COMPAREMOD {
DAYU, DENGYU, XIAOYU
}
public Calculator2() {
// 第几位小数
int bdNum1 = 3;
// 大于等于小于 数字几
int bdNum2 = 4;
boolean blrst = false;
int count = 0;
// 计算4个10以内的数字,共有多少个数满足条件,与合计值相除后,商的第3位小数大于4。
for (BigDecimal i = new BigDecimal(1); i.compareTo(new BigDecimal("1000000000000")) != -1; i
.add(new BigDecimal(1))) {
BigDecimal n0 = i.add(new BigDecimal(0));
BigDecimal n1 = i.add(new BigDecimal(1));
BigDecimal n2 = i.add(new BigDecimal(2));
BigDecimal n3 = i.add(new BigDecimal(3));
BigDecimal n4 = i.add(new BigDecimal(4));
BigDecimal n5 = i.add(new BigDecimal(5));
BigDecimal n6 = i.add(new BigDecimal(6));
BigDecimal n7 = i.add(new BigDecimal(7));
BigDecimal n8 = i.add(new BigDecimal(8));
BigDecimal n9 = i.add(new BigDecimal(9));
if (doHalfAdjustRecycle2(bdNum1, bdNum2, COMPAREMOD.DAYU, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9)) {
count += 1;
blrst = true;
}
}
if (blrst) {
System.out.println("恭喜你,找到了" + count + "个这样的数!");
} else {
System.out.println("计算结束,没找到!");
}
}
/**
* 指定任意个数的数字,计算它们分别与合计的值进行除算,得到的商中满足条件的组合。
*
* @param targetRecycle 随机循环次数
* @param currentRecycle 随机循环当前次数
* @param compareNumIndex 第几位小数
* @param comparedNum 和哪个数字进行比较
* @param mode 比较是大于等于还是等于
* @return
*/
private boolean doHalfAdjustRecycle2(int compareNumIndex, int comparedNum, COMPAREMOD mode,
BigDecimal... numParamArray) {
// numParamArray的合计
// BigDecimal sum = new BigDecimal(IntStream.of(numParamArray).sum());
BigDecimal sum = new BigDecimal(0);
for (int i = 0; i < numParamArray.length; i++) {
sum = sum.add(numParamArray[i]);
}
// 商的集合
BigDecimal[] thres = new BigDecimal[numParamArray.length];
for (int i = 0; i < thres.length; i++) {
// 求每一个数除以合计的商,第compareNumIndex+3位小数四舍五入。
thres[i] = numParamArray[i].divide(sum, compareNumIndex + 3, RoundingMode.FLOOR);
// 求第compareNumIndex位小数
BigDecimal locThreshold = new BigDecimal(
thres[i].toString().substring(compareNumIndex + 1, compareNumIndex + 2));
// 如果第compareNumIndex位的小数值不满足要求,则返回false,终止程序
if (!compare(new BigDecimal(comparedNum), mode, locThreshold)) {
return false;
}
}
for (int i = 0; i < numParamArray.length; i++) {
System.out.println(
numParamArray[i] + "/" + sum + "≈" + thres[i].toString().substring(0, compareNumIndex + 2));
}
System.out.println("****************\n");
return true;
}
/**
* num与comparedNum做比较
*
* @param comparedNum 正整数
* @param mode 比较模式:大于,等于,小于
* @param num 正整数
* @return
*/
private boolean compare(BigDecimal comparedNum, COMPAREMOD mode, BigDecimal... numParamArray) {
for (int i = 0; i < numParamArray.length; i++) {
BigDecimal compareSrc = numParamArray[i];
if (mode == COMPAREMOD.DAYU) {
if (compareSrc.compareTo(comparedNum) != 1)
return false;
} else if (mode == COMPAREMOD.DENGYU) {
if (compareSrc.compareTo(comparedNum) != 0)
return false;
} else {
if (compareSrc.compareTo(comparedNum) != -1)
return false;
}
}
return true;
}
public static void main(String[] args) {
Calculator2 c = new Calculator2();
}
}