- ClassName: bb
- Package: PACKAGE_NAME
- Description:
- [二、阶乘数字和]
- n!的意思是 nx(n - 1) x ... x 3x2x 1例如,10! = 10 x9 x .. x 3 x 2x 1 = 3628800,
- 所以10!的各位数字和是 3+6+2+8+8+0+0=27。求出 100!的冬位数字和。
- @Author: 阿旭
- @Create: 2023/9/14 - 21:56
- @Version: v1.0
*/
public class factorial {
public static void main(String[] args) {
int num = 100;
BigInteger fac = factorialFun(num);
System.out.println(fac);
System.out.println(sum(fac));
}
//求100! public static BigInteger factorialFun(int num){ BigInteger result = BigInteger.ONE; for (int i = 1; i <= num; i++) { result = result.multiply(BigInteger.valueOf(i)); } return result; } //计算100!的值的各位数的和 public static int sum(BigInteger fac){ String str = fac.toString(); int sum = 0; for (int i = 0; i < str.length(); i++) { //Character.getNumericValue获取表示数字字符的字符的整数值。 //例如,如果你有一个字符 '5',你可以使用 Character.getNumericValue('5') 来获取整数值 5。 sum += Character.getNumericValue(str.charAt(i)); } return sum; } }
package com.axu.exer9_15;/**
- ClassName: MaxProduct
- Package: com.axu.exer9_15
- Description:
- 【二、连续数字最大乘积】
- 在下面这个1000位正整数中,连续4个数字的最大乘积是 9×9×8×9= 5832。
- 73167176531330624919225119674426574742355349194934
- 96983520312774506326239578318016984801869478851843
- 85861560789112949495459501737958331952853208805511
- 12540698747158523863050715693290963295227443043557
- 66896648950445244523161731856403098711121722383113
- 62229893423380308135336276614282806444486645238749
- 30358907296290491560440772390713810515859307960866
- 70172427121883998797908792274921901699720888093776
- 65727333001053367881220235421809751254540594752243
- 52584907711670556013604839586446706324415722155397
- 53697817977846174064955149290862569321978468622482
- 83972241375657056057490261407972968652414535100474
- 82166370484403199890008895243450658541227588666881
- 16427171479924442928230863465674813919123162824586
- 17866458359124566529476545682848912883142607690042
- 24219022671055626321111109370544217506941658960408
- 07198403850962455444362981230987879927244284909188
- 84580156166097919133875499200524063689912560717606
- 05886116467109405077541002256983155200055935729725
- 71636269561882670428252483600823257530420752963450
- 找出这个1000位正整数中乘积最大的连续13个数字。它们的乘积是多少?
- @Author: 阿旭
- @Create: 2023/9/15 - 23:22
- @Version: v1.0 */ public class MaxProduct { public static void main(String[] args) { String str = "73167176531330624919225119674426574742355349194934" + "96983520312774506326239578318016984801869478851843" + "85861560789112949495459501737958331952853208805511" + "12540698747158523863050715693290963295227443043557" + "66896648950445244523161731856403098711121722383113" + "62229893423380308135336276614282806444486645238749" + "30358907296290491560440772390713810515859307960866" + "70172427121883998797908792274921901699720888093776" + "657273330010533”
- System.out.println("连续13位数最大乘积为:" + num(str));
}
//字符串截取找到最大的13位数
public static Long num(String str){
Long maxProduct = 1L;
String maxN = "";
for (int i = 0; i < str.length()-13; i++) {
String substring = str.substring(i, i+13);
long product = maxProduct(substring);
if (product > maxProduct){
maxProduct = product;
maxN = substring;
}
}
System.out.println("乘积最大的连续13位数是:" + maxN);
return maxProduct;
}
public static long maxProduct(String substring){
long product = 1L;
//提取字符串的每个字符
for (int i = 0; i < substring.length(); i++) {
//Character.getNumericValue获取表示数字字符的字符的整数值。
//例如,如果你有一个字符 '5',你可以使用 Character.getNumericValue('5') 来获取整数值 5。
product *= Character.getNumericValue(substring.charAt(i));
}
return product;
}
}
package com.axu.exer9_14;
/**
- ClassName: ${NAME}
- Package:
- Description:
- 2023-9-14 编程题练习
- [一、可截取素数]
- 3797 有着奇特的性质。不仅它本身是一个素数,而且如果从左往右逐一截去数字,
- 剩下的仍然都是素数: 3797、797、97 和 7;同样地,如果从右往左逐一截去数字,
- 剩下的也依然都是素数: 3797、379、37 和 3。只有 11 个素数,无论从左往右还是从右往左逐一截去数字,
- 剩下的仍然都是素数,求这些数的和。注意: 2、3、5 和 7 不被视为可截素数。
- 解析:除了2、3、5 和 7 ,继续找到十一个这种奇特性质的素数
- @Author: 阿旭
- @Create: ${DATE} - ${TIME}
- @Version: v1.0
*/
public class subPrime {
public static void main(String[] args) {
//一个若干位素数,无论从左往右还是从右往左逐一截去数字,剩下的仍然是素数
int num = 10;
int count = 0;
int sum = 0;
while (count < 11){
if (isPrime(num)){
if(subStr(num)){
sum += num;
count++;
}
}
num++;
}
System.out.println(sum);
}
//判断素数 public static boolean isPrime(int n){ if (n <= 1){ return false; }else if (n ==2 || n == 3){ return true; }else { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0){ return false; } } return true; } }
//截取 public static boolean subStr(int num){ String str = String.valueOf(num); int len = str.length(); //从左到右逐一截取 for (int i = 0; i < len; i++) { String strNewL = str.substring(i);//一个参数,i到最后一位索引 int i1 = Integer.parseInt(strNewL); if (!isPrime(i1)){ return false; } } //从右到左逐一截取 for (int i = 0; i < len; i++) { String strNewR = str.substring(0,len-i);//两个参数,左闭右开 int i1 = Integer.parseInt(strNewR); if (!isPrime(i1)){ return false; } } return true; } }
package com.axu.exer9_14;
import java.math.BigInteger;
/**
- ClassName: bb
- Package: PACKAGE_NAME
- Description:
- [二、阶乘数字和]
- n!的意思是 nx(n - 1) x ... x 3x2x 1例如,10! = 10 x9 x .. x 3 x 2x 1 = 3628800,
- 所以10!的各位数字和是 3+6+2+8+8+0+0=27。求出 100!的冬位数字和。
- @Author: 阿旭
- @Create: 2023/9/14 - 21:56
- @Version: v1.0
*/
public class factorial {
public static void main(String[] args) {
int num = 100;
BigInteger fac = factorialFun(num);
System.out.println(fac);
System.out.println(sum(fac));
}
//求100! public static BigInteger factorialFun(int num){ BigInteger result = BigInteger.ONE; for (int i = 1; i <= num; i++) { result = result.multiply(BigInteger.valueOf(i)); } return result; } //计算100!的值的各位数的和 public static int sum(BigInteger fac){ String str = fac.toString(); int sum = 0; for (int i = 0; i < str.length(); i++) { //Character.getNumericValue获取表示数字字符的字符的整数值。 //例如,如果你有一个字符 '5',你可以使用 Character.getNumericValue('5') 来获取整数值 5。 sum += Character.getNumericValue(str.charAt(i)); } return sum; } }