62,614
社区成员
发帖
与我相关
我的任务
分享
public class ModApplication {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
//输出到指定文件
PrintWriter writer=new PrintWriter("D://aotomorphic_numbers.txt","utf-8");
int limit=10000000;
printAutomorphicNumbers(limit);
}
//打印所有的自守数到控制台
private static void printAutomorphicNumbers(int limit) throws FileNotFoundException, UnsupportedEncodingException {
IntStream.range(0,limit+1).parallel().filter(ModApplication::passed).forEach(i->System.out.print(i+" "));
}
//计算自守数个数,串行处理
private static long countAutomorphicNumbers(int limit) throws FileNotFoundException, UnsupportedEncodingException {
return IntStream.range(0,limit+1).filter(ModApplication::passed).count();
}
//打印所有的自守数到文件,文件由writer指定,采用了并行处理
private static void printAutomorphicNumbers(int limit,PrintWriter writer) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter out=writer;
IntStream.range(0,limit+1).parallel().filter(ModApplication::passed).forEach(i->out.print(i+" "));
}
//自守数需要满足的条件
private static boolean passed(int x){
int y=x%10;
if(y==0||y==1||y==5||y==6){
return true;
}
return false;
}
}
IntStream.range(0, 10000000).filter(index -> BigDecimal.valueOf(index).pow(2).toString().endsWith(String.valueOf(index))).forEach(System.out::println);
public class Test {
public static void main(String[] args) {
System.out.println(0);
IntStream.range(0, 10000000).filter(i -> i % 10 == 1 || i % 10 == 5 || i % 10 == 6).filter(Test::check).forEach(System.out::println);
}
private static boolean check(long index) {
// 可以获取超过long长度的值
String powValue = BigDecimal.valueOf(index).pow(2).toString();
int len = powValue.length();
while (index != 0 && powValue.charAt(--len) == index % 10 + '0') {
index /= 10;
}
return index == 0;
}
}
0
1
5
6
25
76
376
625
9376
90625
109376
890625
2890625
7109376
// 自守数 5*5=25 自守数是指某个数的平方的末尾几位数等于这个数的数
public static void zsNum() {
for (long i = 1; i < 10000000; i++) {
String firstStr = String.valueOf(i);// 首位
String multiStr = String.valueOf(i * i);// 自己相乘后的结果
String lastStr = multiStr.substring(multiStr.length() - firstStr.length());// 末位
if (firstStr.equals(lastStr)) {// 判断首位和末位是否相等
System.out.printf("%s 是自守数 ==> %s*%s=%s", firstStr, firstStr, firstStr, multiStr);
System.out.println();
}
}
}