problem 23

问题描述:

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

<!-- <p>A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant.</p> -->

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


解决方法:

 

	public static int sum_divisors(int number){
	
		int result = 1;
		if(number%2!=0){
			for(int i=3; i*i<=number; i+=2){
				if(number%i==0){
					result +=i;
					int other_i = number/i;
					if(i!=other_i&&number%other_i==0){
						result +=other_i;
					}
				}
			}
		}else{
			int two = 2;
			int step =0;
			int divide =1;
			int original = number;
			while(number%2==0){
				number = number/2;
				divide *=2;
				step++;
			}
			if(number==1){
				step--;
			}
			for(int i=0; i<step; i++){
				result += two;
				two *=2;
			}
			number = original/divide;
//			System.out.println(step+"_"+number+"_"+result);
			for(int i=3; i<=number; i+=2){
				if(number%i==0){
					result +=i;
					int begin = 2;
					for(int j=0; j<step; j++){
						if(i*begin<original){
							result += i*begin;
							begin *=2;
						}else{
							break;
						}
					}
				}
			}
		}
		
		return result ;
	}
	
	public static int total(){
		int result = 0;
		
		Set<Integer> abundant = new HashSet<Integer>();
		
		for(int i=4; i<UPPER; i++){
			if(sum_divisors(i)>i)
				abundant.add(i);
		}
		
		for(int i=1; i<UPPER; i++){
			int middle = i/2;
			boolean ok = true;
			for(int j=4; j<=middle; j++){
				if(abundant.contains(j)&&abundant.contains(i-j)){
					ok = false;
					break;
				}
			}
			if(ok){
				result += i;
			}
		}
		return result;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值