Logic php condition sum of the divisors - php
I have an array of numbers 1 until 100 and I need to find a list of numbers applying two conditions, first sum of divisors is greater than itself and second condition no subset of divisors can not sums the number itself
for that I have the php code
$arrayNumbers = [];
//array numbers 1 - 100
$a = 1;
$b = 100;
for ($n = $a; $n <= $b; $n++) {
$conditionOne = false; // Condition one: sum of the divisors greater than ifself
$conditionTwo = false; // Condition two: No subset of those divisors sums itself
$multiples = [];
$subset = [];
// Get sum of multiples divisors
for ($i = 1; $i < $n; $i++) {
if ($n % $i == 0)
$multiples[]= $i;
}
//Condition one sum of the divisors greater than itself
if (array_sum( $multiples ) > $n)
$conditionOne = true;
foreach ($multiples as $number) {
if ($number % 2 == 0) $subset[]= $number;
}
// Condition tow sum of the divisors greater than itself
if (array_sum($subset) > $n)
$conditionTwo = true;
// If first ondition one match with second condition two
if ($conditionOne && $conditionTwo)
$arrayNumbers[] = $n;
}
echo implode('</br>$arrayNumbers );
Is this php logic correct? thanks in advance.
Related
Optimizing Find nearest sum of numbers in array to a given number
Say I have an array [10000,5000,1000,1000] and I would like to find the closest sum of numbers to a given number. Sorry for the bad explanation but here's an example: Say I have an array [10000,5000,1000,1000] I want to find the closest numbers to, say 6000. Then the method should return 5000 and 1000 another example : we want the closest to 14000 , so then he should return 10000 and 5000 I've tried with code below, here is working one but if the $desiredSum and $numbers array is big. it's running so slow until php execution timeout $numbers = array( 10000,5000,1000,1000 ); $desiredSum = 6000; $minDist = null; $minDist_I = null; // Iterate on every possible combination $maxI = pow(2,sizeof($numbers)); for($i=0;$i<$maxI;$i++) { if(!(($i+1) % 1000)) echo "."; // Figure out which numbers to select in this $sum = 0; for($j=0;$j<sizeof($numbers);$j++) { if($i & (1 << $j)) { $sum += $numbers[$j]; } } $diff = abs($sum - $desiredSum); if($minDist_I === null || $diff < $minDist) { $minDist_I = $i; $minDist = $diff; } if($diff == 0) break; } $chosen = array(); for($j=0;$j<sizeof($numbers);$j++) { if($minDist_I & (1 << $j)) $chosen[] = $numbers[$j]; } echo "\nThese numbers sum to " . array_sum($chosen) . " (closest to $desiredSum): "; echo implode(", ", $chosen); echo "\n"; Anyone can help me out ?
<?php function coinChange($numbers,$desiredSum){ sort($numbers); $set = []; $set[0] = []; for($i = $numbers[0];$i <= $desiredSum;++$i){ foreach($numbers as $index => $current_number){ if($i >= $current_number && isset($set[$i - $current_number])){ if(isset($set[$i - $current_number][$index])) continue; $set[$i] = $set[$i - $current_number]; $set[$i][$index] = true; break; } } } if(count($set) === 0){ return [0,[]]; } if(isset($set[$desiredSum])){ return [ $desiredSum, formatResult($numbers,array_keys($set[$desiredSum])) ]; }else{ $keys = array_keys($set); $nearestSum = end($keys); $sum = 0; $rev_numbers = array_reverse($numbers); $result = []; foreach($rev_numbers as $number){ $sum += $number; $result[] = $number; if($sum > $nearestSum && abs($nearestSum - $desiredSum) > abs($sum - $desiredSum)){ $nearestSum = $sum; break; }else if($sum > $nearestSum && abs($nearestSum - $desiredSum) < abs($sum - $desiredSum)){ $result = formatResult($numbers,array_keys($set[$nearestSum])); break; } } return [ $nearestSum, $result ]; } } function formatResult($numbers,$keys){ $result = []; foreach($keys as $key) $result[] = $numbers[$key]; return $result; } print_r(coinChange([10000,5000,1000,1000],14000)); print_r(coinChange([10000,5000,1000,1000],13000)); print_r(coinChange([100000,100000,100000,100000,100000,100000,50000,50000,50000,50000,10000,10000,500,500,500,1000,1000],250000)); print_r(coinChange([100000,100000,100000,100000,100000,100000,50000,50000,50000,50000,10000,10000,500,500,500,1000,1000],179999)); Demo: https://3v4l.org/hBGeW Algorithm: This is similar to coin change problem. We first sort the numbers. Now, we iterate from minimum number in the array to the desired sum. Inside it, we iterate through all elements in the array. Now, we can make $i(which is a sum) only if we have made sum $i - $current_number. If we have the previous one, then we add $current_number to our collection for sum $i. Two Scenarios: If we can make the exact sum, then we return the result as is. If we can't, then are 2 possibilities: We would already have nearest sum possible in our $set which would be the last entry. We keep them in a variable. Now, the nearest sum could also be higher than the desired sum. So, we get the larger sum and check if it's nearer than nearest smallest sum and then compare both and return the result. Result format: Let's take the below sample output: Array ( [0] => 15000 [1] => Array ( [0] => 10000 [1] => 5000 ) ) It simply means that the first index is the nearest sum possible and array at 2nd index is all elements it took from $numbers to make that sum.
List of numbers until n divisible by A or B but not divisible by C
I have three integers: A, B, C I want to print all integers from 1 to range which are divisible by A or B but not by C. My code for($n=0; $n < $range; $n++){ if(($n < $a && $n < $b) || ($n % $c == 0)){ return []; } if(($n % $a == 0 || $n % $b == 0) && ($n % $c > 0)){ $outputArr[] = $n; } } Is there any more efficient way to do this?
You can speed this up but it is more complicated, especially if you must print these out in order. Here is a method that doesn't print them out in order. First write a greatest common divisor (gcd) function in PHP, and then write a least common multiple (lcm) function that uses the gcd function. Compute m = lcm(a, b). Iterate over multiples of a and print them out if they are not divisible by c. Next, iterate over multiples of b and print them out if they are not divisible by m or c. Other optimizations along these lines are possible. For example, you can precompute the multiples of a or b that are not multiples of m and store them in an array. This works if m is not too large, division is more expensive than array access in PHP, and range is significantly larger than m.
PHP version 7 or higher is so fast when only integer operations are used that micro-optimizations are no longer needed. $res = []; $a = 9; $b = 13; $c = 26; $range = 10000; for($n=$a; $n <= $range; $n += $a){ if($n%$c != 0) $res[] = $n; } for($n=$b; $n <= $range; $n += $b){ if($n%$c != 0) $res[] = $n; } $res = array_unique($res); sort($res); This example takes about 1 millisecond to calculate the 1411 values on my 8-year-old system. This time for the presentation of the result is several times greater.
I would use range() and array_filter(). $range = 20; $A = 2; $B = 3; $C = 9; $nums = array_filter(range(1, $range), function ($x) use ($A, $B, $C) { return (!($x % $A) || !($x % $B)) && $x % $C; }); var_dump($nums);
Here is a more optimized solution, that also works efficient when a and b are large. You can simply run through the multiples of a and b: for($na=$a, $nb=$b; $na <= $range || $nb <= $range; ){ if ($na <= $nb) { if ($na % $c != 0) $outputArr[] = $na; if ($na == $nb) $nb += $b; $na += $a; } else { if ($nb % $c != 0) $outputArr[] = $nb; $nb += $b; } } Each output number is only generated once, and already in the desired order. If you are afraid the modulo test is slow, you could also have a next multiple of c running along, but that looks like too much overhead.
Project Euler #23: Non-abundant sums
I'm struggling with Project Euler problem 23: Non-abundant sums. I have a script, that calculates abundant numbers: function getSummOfDivisors( $number ) { $divisors = array (); for( $i = 1; $i < $number; $i ++ ) { if ( $number % $i == 0 ) { $divisors[] = $i; } } return array_sum( $divisors ); } $limit = 28123; //$limit = 1000; $matches = array(); $k = 0; while( $k <= ( $limit/2 ) ) { if ( $k < getSummOfDivisors( $k ) ) { $matches[] = $k; } $k++; } echo '<pre>'; print_r( $matches ); I checked those numbers with the available on the internet already, and they are correct. I can multiply those by 2 and get the number that is the sum of two abundant numbers. But since I need to find all numbers that cannot be written like that, I just reverse the if statement like this: if ( $k >= getSummOfDivisors( $k ) ) This should now store all, that cannot be created as the sum of to abundant numbers, but something is not quit right here. When I sum them up I get a number that is not even close to the right answer. I don't want to see an answer, but I need some guidelines / tips on what am I doing wrong ( or what am I missing or miss-understanding ). EDIT: I also tried in the reverse order, meaning, starting from top, dividing by 2 and checking if those are abundant. Still comes out wrong.
An error in your logic lies in the line: "I can multiply those by 2 and get the number that is the sum of two abundant numbers" You first determine all the abundant numbers [n1, n2, n3....] below the analytically proven limit. It is then true to state that all integers [2*n1, 2*n2,....] are the sum of two abundant numbers but n1+n2, and n2+n3 are also the sum of two abundant numbers. Therein lies your error. You have to calculate all possible integers that are the sum of any two numbers from [n1, n2, n3....] and then take the inverse to find the integers that are not.
I checked those numbers with the available on the internet already, and they are correct. I can multiply those by 2 and get the number that is the sum of two abundant numbers. No, that's not right. There is only one abundant number <= 16, but the numbers <= 32 that can be written as the sum of abundant numbers are 24 (= 12 + 12), 30 (= 12 + 18), 32 (= 12 + 20). If you have k numbers, there are k*(k+1)/2 ways to choose two (not necessarily different) of them. Often, a lot of these pairs will have the same sum, so in general there are much fewer than k*(k+1)/2 numbers that can be written as the sum of two of the given k numbers, but usually, there are more than 2*k. Also, there are many numbers <= 28123 that can be written as the sum of abundant numbers only with one of the two abundant numbers larger than 28123/2. This should now store all, that cannot be created as the sum of to abundant numbers, No, that would store the non-abundant numbers, those may or may not be the sum of abundant numbers, e.g. 32 is a deficient number (sum of all divisors except 32 is 31), but can be written as the sum of two abundant numbers (see above). You need to find the abundant numbers, but not only to half the given limit, and you need to check which numbers can be written as the sum of two abundant numbers. You can do that by taking all pairs of two abundant numbers (<= $limit) and mark the sum, or by checking $number - $abundant until you either find a pair of abundant numbers or determine that none sums to $number. There are a few number theoretic properties that can speed it up greatly.
Below is php code takes 320 seconds <?php set_time_limit(0); ini_set('memory_limit', '2G'); $time_start = microtime(true); $abundantNumbers = array(); $sumOfTwoAbundantNumbers = array(); $totalNumbers = array(); $limit = 28123; for ($i = 12; $i <= $limit; $i++) { if ($i >= 24) { $totalNumbers[] = $i; } if (isAbundant($i)) { $abundantNumbers[] = $i; } } $countOfAbundantNumbers = count($abundantNumbers); for ($j = 0; $j < $countOfAbundantNumbers; $j++) { if (($j * 2) > $limit) break; //if sum of two abundant exceeds limit ignore that for ($k = $j; $k < $countOfAbundantNumbers; $k++) { //set $k = $j to avoid duble addtion like 1+2, 2+1 $l = $abundantNumbers[$j] + $abundantNumbers[$k]; $sumOfTwoAbundantNumbers[] = $l; } } $numbers = array_diff($totalNumbers, $sumOfTwoAbundantNumbers); echo '<pre>';print_r(array_sum($numbers)); $time_end = microtime(true); $execution_time = ($time_end - $time_start); //execution time of the script echo '<br /><b>Total Execution Time:</b> ' . $execution_time . 'seconds'; exit; function isAbundant($n) { if ($n % 12 == 0 || $n % 945 == 0) { //first even and odd abundant number. a multiple of abundant number is also abundant return true; } $k = round(sqrt($n)); $sum = 1; if ($n >= 1 && $n <= 28123) { for ($i = 2; $i <= $k; $i++) { if ($n % $i == 0) $sum+= $i + ( $n / $i); if ($n / $i == $i) { $sum = $sum - $i; } } } return $sum > $n; }
Dividing a integer equally in X parts
I'm looking for a efficient way in PHP to divide a number in equal part. Number will always be integer (no float). Let's say that I have an array $hours with values from "1" to "24" ($hours['1'], etc) and a variable $int containing an integer. What I want to acheive is spreading the value of $int equally in 24 parts so I can assing the value to each corresponding array entries. (Should the number be odd, the remaining would be added to the last or first values in the 24). Regards,
Here's the algorithm you're looking for; it evenly spreads an integer N over K cells: for i = 0 to K array[i] = N / K # integer division # divide up the remainder for i = 0 to N mod K array[i] += 1
Try this code <?php $num = 400; $val = floor($num/24); for($i=0;$i<24;$i++) { $arr[$i] = $val; } $arr[0] += $num - array_sum($arr); ?>
function split($x, $n) { // If we cannot split the // number into exactly 'N' parts if($x < $n) echo (-1); // If x % n == 0 then the minimum // difference is 0 and all // numbers are x / n else if ($x % $n == 0) { for($i = 0; $i < $n; $i++) { echo ($x / $n); echo (" "); } } else { // upto n-(x % n) the values // will be x / n // after that the values // will be x / n + 1 $zp = $n - ($x % $n); $pp = $x / $n; for ($i = 0; $i < $n; $i++) { if($i >= $zp) { echo (int)$pp + 1; echo (" "); } else { echo (int)$pp; echo (" "); } } } } // Driver code $x = 5; $n = 3; split( $x, $n);
how to find the sum of all the multiples of 3 or 5 below 1000 in php, issue?
i have an small issue with the way this problem is resolved. some would say: println((0 /: ((0 until 1000).filter(x => x % 3 == 0 || x % 5 == 0))) (_+_)) is the solution witch adds to 233168 my way was to do: $maxnumber = 1000; for ($i = 3; $i < $maxnumber; $i += 3) { $t += $i; echo $i.','; } echo '<br>'; for ($j = 5; $j < $maxnumber; $j += 5) { $d += $j; echo $j.','; } echo '<br>'; echo $t; echo '<br>'; echo $d; echo '<br>'; echo $t+$d; this will give me : 3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312,315,318,321,324,327,330,333,336,339,342,345,348,351,354,357,360,363,366,369,372,375,378,381,384,387,390,393,396,399,402,405,408,411,414,417,420,423,426,429,432,435,438,441,444,447,450,453,456,459,462,465,468,471,474,477,480,483,486,489,492,495,498,501,504,507,510,513,516,519,522,525,528,531,534,537,540,543,546,549,552,555,558,561,564,567,570,573,576,579,582,585,588,591,594,597,600,603,606,609,612,615,618,621,624,627,630,633,636,639,642,645,648,651,654,657,660,663,666,669,672,675,678,681,684,687,690,693,696,699,702,705,708,711,714,717,720,723,726,729,732,735,738,741,744,747,750,753,756,759,762,765,768,771,774,777,780,783,786,789,792,795,798,801,804,807,810,813,816,819,822,825,828,831,834,837,840,843,846,849,852,855,858,861,864,867,870,873,876,879,882,885,888,891,894,897,900,903,906,909,912,915,918,921,924,927,930,933,936,939,942,945,948,951,954,957,960,963,966,969,972,975,978,981,984,987,990,993,996,999 5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320,325,330,335,340,345,350,355,360,365,370,375,380,385,390,395,400,405,410,415,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490,495,500,505,510,515,520,525,530,535,540,545,550,555,560,565,570,575,580,585,590,595,600,605,610,615,620,625,630,635,640,645,650,655,660,665,670,675,680,685,690,695,700,705,710,715,720,725,730,735,740,745,750,755,760,765,770,775,780,785,790,795,800,805,810,815,820,825,830,835,840,845,850,855,860,865,870,875,880,885,890,895,900,905,910,915,920,925,930,935,940,945,950,955,960,965,970,975,980,985,990,995 $t - 166833 $d - 99500 and total: 266333 why am i wrong?
Some numbers are multiples of both 3 and 5. (Your algorithm adds these numbers to the total twice.)
Because 6 * 5 == 30 and 10 * 3 == 30, you're adding the some numbers up twice. $sum = 0; $i = 0; foreach(range(0, 999) as $i) { if($i % 3 == 0 || $i % 5 == 0) $sum += $i; }
Because you double-count numbers that are multiple of both 3 and 5, i.e. multiples of 15. You can account for this naively by subtracting all multiples of 15. for ($j = 15; $j < $maxnumber; $j += 15) { $e += $j; echo $j.','; } $total = $total - $d;
In your case, if it is 15, you will add the number twice. Try this: $t = 0; $d = 0; for ($i = 0; $i <= $maxnumber; $i++){ if ($i % 3 == 0) $t+= $i; else if ($i % 5 == 0) $d += $i; } echo $t.'<br>'.$d;
I think that in your code, if a number is a multiple of 3 and 5, it is added twice. Take 15 for example. It's in your list of multiples of 3 and in the list of multiples of 5. Is this the behaviour you want?
One of the best approach to this solution (to achieve optimum time complexity), run an Arithmetic Progression series and find the number of terms in all series by using AP formula: T=a+(n-1)d, then find sum by : S=n/2[2*a+(n-1)d] where : a=first term ,n=no. of term , d=common deference, T=nth term The code solution below has been implemented to suit the question above - so the values 3 and 5 are hard-coded. However, the function can modified such that values are passed in as variable parameters. function solution($number){ $val1 = 3; $val2 = 5; $common_term = $val1 * $val2; $sum_of_terms1 = calculateSumofMulitples($val1,$number); $sum_of_terms2 = calculateSumofMulitples($val2,$number); $sum_of_cterms = calculateSumofMulitples($common_term,$number); $final_result = $sum_of_terms1 + $sum_of_terms2 - $sum_of_cterms; return $final_result; } function calculateSumofMulitples($val, $number) { //first, we begin by running an aithmetic prograssion for $val up to $number say N to get the no of terms [using T=a +(n-1)d] $no_of_terms = (int) ($number / $val); if($number % $val == 0) $no_of_terms = (int) ( ($number-1)/$val ); //since we are computing for a no of terms below N and not up to/exactly/up to N. if N divides val with no remainder, use no of terms = (N-1)/val //second, we compute the sum of terms [using Sn = n/2[2a + (n-1)d] $sum_of_terms = ($no_of_terms * 0.5) * ( (2*$val) + ($no_of_terms - 1) * $val ); // sum of multiples return $sum_of_terms; }
You can run a single loop checking whether the number is multiple of 3 OR 5: for ($i = 0; $i < $maxnumber; $i++) { if($i%3 || $i%5){ $t += $i; echo $i.',';} }
I think the original code is not including numbers which are multiples of both 3 and 5 in the total: if the test for multiple of 3 matches, it takes that and goes on. If you total the multiples of 15 up to 1000, you get 33165, which is exactly the difference between your total, 266333, and the original total, 233168.
Here's my solution to the question: <?php $sum = 0; $arr = []; for($i = 1; $i < 1000; $i++){ if((int)$i % 3 === 0 || (int)$i % 5 === 0) { $sum += $i; array_push($arr,$i); } } echo $sum; echo '<br>'; print_r($arr);//Displays the values meeting the criteria as an array of values