Solved ProjectEuler/023: php
This commit is contained in:
		
							
								
								
									
										17
									
								
								ProjectEuler/023/desc.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								ProjectEuler/023/desc.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| title: Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. | ||||
| url: http://projecteuler.net/problem=23 | ||||
|  | ||||
| desc: | | ||||
|   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. | ||||
|   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. | ||||
| | | ||||
|  | ||||
| todo: Improve algorithm  | ||||
| solution: From we http://mathworld.wolfram.com/AbundantNumber.html we cheat and limit our search to 20161 | ||||
|  | ||||
| solutions: | ||||
|   solve.php: | ||||
|     desc: Very slow - takes around 1 minut on my Core 2 Duo | ||||
|     language: php | ||||
							
								
								
									
										27
									
								
								ProjectEuler/023/solve.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								ProjectEuler/023/solve.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| <?php | ||||
| function abundant($input) { | ||||
| 	$max = floor(sqrt($input)); | ||||
| 	$sum = 1; | ||||
| 	for($div=2;$div<=$max ;$div++) { | ||||
| 		if($input % $div == 0) { | ||||
| 			$sum += ($input/$div != $div) ? $input/$div + $div : $div; | ||||
| 			if($sum > $input) | ||||
| 				return true; | ||||
| 		} | ||||
| 	} | ||||
| 	return false; | ||||
| } | ||||
| //Find all abundant numbers | ||||
| for($number = 12; $number <= 28123 ; $number++) { | ||||
| 	if(abundant($number)) { $adjnum[] = $number; } | ||||
| } | ||||
|  | ||||
| $sum = array_sum(range(1,23)); | ||||
| for($test = 24; $test <= 20162; $test++) { | ||||
| 	$nadundant = true; | ||||
| 	for($index = 0; $adjnum[$index] < $test; $index++) { | ||||
| 		if(abundant($test - $adjnum[$index])) {$nadundant = false; break; } | ||||
| 	} | ||||
| 	if($nadundant) {$sum += $test; } | ||||
| } | ||||
| echo $sum; | ||||
		Reference in New Issue
	
	Block a user
	 FuryFire
					FuryFire