public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
int sum = 0;
for(int i=1; i<10000000; i++){
int j = String.valueOf(i).length(); //位数
int k = (int)Math.pow(i, 2); //平方数
int l = Integer.parseInt(digit(k, j)); //平方数的最后x位数,x为i的位数
if(i==l){
sum += 1;
}
}
System.out.println(sum);
}
/**
* 求整数的最后x位
* @param num 整数
* @param i 最后x位数
* @return
*/
public static String digit(int num, int i){
StringBuffer sBuffer = new StringBuffer();
int j = String.valueOf(num).length();
if (j < i) {
return sBuffer.append(0).toString();
}
if (j == i) {
return sBuffer.append(num).toString();
}
for(int x=1; x<=i; x++){
int gewei = num%10;
num = num/10;
sBuffer.append(gewei);
}
return sBuffer.reverse().toString();
}
for (long j = 0; j < 10000000; j++) {
if (String.valueOf(j*j).endsWith(String.valueOf(j))) {
System.out.println(j);
}
}
public class Demo1 {
public static void main(String[] args){
for(int i = 1; i<1000;i++){
if(i < 10){
if((i * i - i) % 10 == 0){
System.out.println(i);
}
}
if(i < 100){
if((i * i - i) % 100 == 0){
System.out.println(i);
}
}
if(i < 1000){
if((i * i - i) % 1000 == 0){
System.out.println(i);
}
}
}
}
}
public class Demo1 {
public static void main(String[] args){
for(int i = 1; i<1000;i++){
if(i % 10 != 5 && i % 10 != 6){
continue;
}
if(i < 10){
if((i * i - i) % 10 == 0){
System.out.println(i);
}
}else if(i < 100){
if((i * i - i) % 100 == 0){
System.out.println(i);
}
}else{
if((i * i - i) % 1000 == 0){
System.out.println(i);
}
}
}
}
}
public class SelfNum {
public static boolean isSelfNum(BigInteger n,BigInteger mod)
{
return n.multiply(n.subtract(BigInteger.ONE)).mod(mod).equals(BigInteger.ZERO);
}
public static void main( String[] args ) {
int k = 300;
List<BigInteger> list = new ArrayList<BigInteger>();
list.add(BigInteger.ONE);
list.add(BigInteger.valueOf(5));
list.add(BigInteger.valueOf(6));
BigInteger offset = BigInteger.ONE;
for(int i = 2 ; i <= k; i++)
{
offset = offset.multiply(BigInteger.TEN);
int size = list.size();
for(int j = 0; j < size; ++j)
{
for(int x = 1; x < 10; ++ x)
{
// tmp = offset * x + list(j)
BigInteger tmp = offset.multiply(BigInteger.valueOf(x)).add(list.get(j));
if(isSelfNum(tmp, offset.multiply(BigInteger.TEN)))
list.add(tmp);
}
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
/**
* 思路:n的平方对整的x次幂取余结果是n,则满足条件
* 例5*5=25 25%10=5
* 625*625=390625 625%(10*10*10)=625
* @author yx
*
* 2017-9-25
*/
public class Test {
public static void main(String[] args) {
for (int j = 0; j < 10000000; j++) {
for (int i = 1; i < String.valueOf(j).length(); i++) {
if (Math.pow(j % (Math.pow(10, i)), 2) == j) {
System.out.println(j + "," + (int) (j % (Math.pow(10, i))));
break;
}
}
}
}
}
public class SelfNum {
public static boolean isSelfNum(BigInteger n,BigInteger mod)
{
return n.multiply(n.subtract(BigInteger.ONE)).mod(mod).equals(BigInteger.ZERO);
}
public static void main( String[] args ) {
int k = 1000,firstEnable = 0, top = 3;
boolean[] enable = new boolean[ 3 * k];
List<BigInteger> list = new ArrayList<BigInteger>();
list.add(BigInteger.ONE);
list.add(BigInteger.valueOf(5));
list.add(BigInteger.valueOf(6));
enable[0] = enable[1] = enable[2] = true;
BigInteger offset = BigInteger.ONE;
for(int i = 2 ; i <= k; i++)
{
offset = offset.multiply(BigInteger.TEN);
int size = list.size();
for(int j = firstEnable; j < size; ++j)
{
if(!enable[j] || !isSelfNum(list.get(j), offset)) {
enable[j] = false;
continue;
}
for(int x = 1; x < 10; ++ x)
{
// tmp = offset * x + list(j)
BigInteger tmp = offset.multiply(BigInteger.valueOf(x)).add(list.get(j));
if(isSelfNum(tmp, offset.multiply(BigInteger.TEN))) {
enable[top++] = true;
list.add(tmp);
}
}
}
while (firstEnable < list.size() && !enable[firstEnable])
firstEnable++;
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
public class SelfNum {
public static boolean isSelfNum(BigInteger n,BigInteger mod)
{
return n.multiply(n.subtract(BigInteger.ONE)).mod(mod).equals(BigInteger.ZERO);
}
public static void main( String[] args ) {
int k = 3000, head = 0, tail = 2;
int[] next = new int[3 * k];
int[] last = new int[3 * k];
List<BigInteger> list = new ArrayList<BigInteger>();
list.add(BigInteger.ONE);
list.add(BigInteger.valueOf(5));
list.add(BigInteger.valueOf(6));
next[0] = 1;next[1] = 2; next[2] = -1;
last[2] = 1;last[1] = 0; last[0] = -1;
BigInteger offset = BigInteger.ONE;
for(int i = 2 ; i <= k; i++)
{
offset = offset.multiply(BigInteger.TEN);
int size = list.size();
for(int j = next[head]; j < size && j != -1; j = next[j])
{
// 保证前i-1位数是自首数
if(!isSelfNum(list.get(j), offset)) {
// 第一个节点肯定不会被删除
next[last[j]] = next[j];
last[next[j]] = last[j];
continue;
}
for(int x = 1; x < 10; ++ x)
{
// tmp = offset * x + list(j)
BigInteger tmp = offset.multiply(BigInteger.valueOf(x)).add(list.get(j));
if(isSelfNum(tmp, offset.multiply(BigInteger.TEN))) {
last[tail + 1] = tail;
next[tail + 1] = -1;
next[tail] = tail + 1;
list.add(tmp);
tail ++;
}
}
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
int max = 10000000;
int k = 0;
for (int n=1; n<max; n++) {
k = (int)Math.log10(n) + 1; // 数的长度
if ( (n%Math.pow(2, k)==0 || (n-1)%Math.pow(2, k)==0) && ((n)%Math.pow(5, k)==0 || (n-1)%Math.pow(5, k)==0) ) {
System.out.println("自守数:" + n);
}
}
Integer num=0;
for (int i = 0; Math.pow(i, 2)<1000000; i++) {
if (i / 100 >=1) { // 百位数
num = (Integer)(i*i);
String a = num.toString().substring(num.toString().length()-3);
if (a.equals(i+"")) {
System.out.println(i);
}
} else if (i / 10 > 1) { // 十位数
num = (Integer)(i*i);
String a = num.toString().substring(num.toString().length()-2);
if (a.equals(i+"")) {
System.out.println(i);
}
} else { //个位数
num = (Integer)(i*i);
String a = num.toString();
if (a.equals(i+"")) {
System.out.println(i);
}
}
}
public class TestSelf {
public static void main(String[] args){
// 个位一定是0、1、5、6,其它的数不用考虑
int[] choice = new int[]{
0, 1, 5, 6
};
// 个位一定是0、1、5、6,所以从10位开始累加
for(int a = 0; a < 10000000 / 10; a++){
int p = a * 10;
for(int c : choice){
// 被测试的数
int v = p + c;
// 被测试的数的平方
long vv = v * v;
// 得到被测试的数的位数
int x = 1;
int v1 = v;
while(v1 > 0){
v1 = v1 / 10;
x *= 10;
}
// 对整10、整百……取余,如果与测试数相等,则是自守数
if(vv % x == v)
System.out.println(v);
}
}
}
}
0
1
5
6
25
76
376
625
9376
public class ZsNum {
public static void main(String[] args) {
long ji=0;
long k=0;
int lgval=0;
System.out.println("0*0=0");
for (int i=1;i<=1000000;i++)
{
ji=(long)i*i;
lgval=(int)Math.log10(i)+1;
if (ji%(long)Math.pow(10, lgval)==i)
{
System.out.println(i+"*"+i+"="+ji);
}
}
}
}