求100万一下自守数

jsbintask22 2017-09-22 10:42:56
加精
昨天的校招 求一千万以下的自守数 如 5*5=25 25*25=625 如果有n由k位数构成 那么n的平方的末尾k位数必须和n相等 并且变量只能用证书 直接for循环是行不通了 有没有老铁有其他解决办法。
...全文
6295 79 打赏 收藏 转发到动态 举报
写回复
用AI写文章
79 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiawenhe123 2019-11-26
  • 打赏
  • 举报
回复
如果个位数是0,1,5,6则是自守数,使用流处理,如果不习惯可以使用循环处理,但是无法自动fork/join.
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;
	}
}
  • 打赏
  • 举报
回复
引用 96 楼 落幕安魂 的回复:
考虑long边界问题,

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


还有一行代码搞定

IntStream.range(0, 10000000).filter(index -> BigDecimal.valueOf(index).pow(2).toString().endsWith(String.valueOf(index))).forEach(System.out::println);
  • 打赏
  • 举报
回复
考虑long边界问题,

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
一般的俗 2019-04-26
  • 打赏
  • 举报
回复

// 自守数 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();
}
}
}
RichardCod 2019-04-08
  • 打赏
  • 举报
回复
为什么不能循环
阿姆特雷 2019-04-07
  • 打赏
  • 举报
回复
观望等待大神中
神之的神 2019-04-06
  • 打赏
  • 举报
回复

public static void main(String[] args) throws Exception{

// TODO Auto-generated method stub



int sum = 0;

for(int i=1; i<10000000; i++){
qq_44875111 2019-04-04
  • 打赏
  • 举报
回复
什么东西搞不懂
SBM8888 2019-04-02
  • 打赏
  • 举报
回复
这个不是很清楚
無名小卒 2019-03-22
  • 打赏
  • 举报
回复
感谢楼主的分享!
性格缺陷 2019-03-21
  • 打赏
  • 举报
回复
66666666
kuku964 2019-03-11
  • 打赏
  • 举报
回复
学习下,谢谢
pro_or_check 2019-03-02
  • 打赏
  • 举报
回复


短短一段蚊子,你出两个错字,海是别当程序员了。
qq_35742086 2019-02-25
  • 打赏
  • 举报
回复
这是个高手回答的问题
鹰皇 2019-02-25
  • 打赏
  • 举报
回复
脑残了,应该是n/2位
鹰皇 2019-02-25
  • 打赏
  • 举报
回复
可以证明,如果一个n位数abcde……是一个自守数,则n-2位数cdef……也是一个自守数,反过来如果已知一个自守数abcd,也我们只要求它的前两位就好了,只需要100次循环,然后如果在这100次循环中找到了qwabcd和mnabcd,则进一步以这两个为后六位继续循环,这样就大大减少了计算量
崇仰 2019-02-18
  • 打赏
  • 举报
回复
你发了个锤子?
chuiwei 2019-02-12
  • 打赏
  • 举报
回复
很好。很优秀
qq_15545731 2019-02-10
  • 打赏
  • 举报
回复
膜拜膜拜大神
qq_35859210 2019-02-04
  • 打赏
  • 举报
回复
感觉还可以,我是来蹭积分的。
加载更多回复(59)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧