Project Euler Spoilers, #001 - PHP Sums - php

I cannot ask for help on their forums, but i've been at this for 3 hours now. Spoilers Below I don't understand what i'm doing wrong. The question is:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Here's my equation I made.
for($total = 0, $f = 5, $t = 3; $t < 1000; $t+=3){
if($f < 1000)
{
$total += $f + $t;
echo "Five: $f, Three: $t = $total<br />";
$f += 5;
}
else
{
$total += $t;
echo "Five: $f, Three: $t = $total<br />";
}
}
The answer is:233168. Where's my error?

You are counting numbers that are divisible both by 3 and 5 twice.

Suppose S(3) denotes sum of numbers divisible by 3 and S(5) denotes sum of numbers divisible by 5 till a given number n, then sum of numbers divisible by 3 or 5 is given by
S(3 U 5) = S(3) + S(5) - S(3 ∩ 5)
where S(3 ∩ 5) denotes sum of those numbers divisible by both 3 and 5.
In your case, you are calculating S(3 U 5) = S(3) + S(5) and hence getting wrong answer.

Related

find first occurence of a sum of digits in an endless number

For an endless number such as Pi, how would one go about finding the first occurrence of an exact sum of digits for a given number n.
For example. If n=20
Pi=3.14159265358979323846264338327950288419716939...
then the first occurrence is from digit 1 to digit 5 since:
1+4+1+5+9=20
if n=30, then the first occurrence is from digit 5 to digit 11
since 9+2+6+5+3+5=30
answer should have a working php demo
The answer to this is using sliding window that will maintain the sum. so maintain two pointers say i and j. Keep increasing j and adding the elements inside. when it crosses the desired sum increase i and decrease the element at i. Then keep increasing j until the sum is reached or the sum overflows so you repeat the above process.
Example sum = 30
141592653589793238 >> i=j=0 current_sum = 1
141592653589793238 >> i=0 j=6 current_sum=28
in the next iteration adding 5 will result in current_sum>30 so hence you increment i
141592653589793238 >> i=1 j=6 current_sum=27
141592653589793238 >> i=2 j=6 current_sum=23
141592653589793238 >> i=2 j=7 current_sum=28
Keep going in this manner and it will finally reach the window that is equal to the sum =30 . That should break you out of the loop and help you find the answer.
Method 1 (suggested by Ashwin Bhat)
This implementation uses two pivots. The sum of digits between $pivot_a and $pivot_b is computed. Depending on the value of the sum, we increment $pivot_b (if the sum is less) or $pivot_a (if the sum is greater). If the sum is equal to $n, break. The values of the pivots give the appropriate digit indices.
$pi = "314159265358979323846264338327950288419716939";
$n = 30;
$pivot_a = $pivot_b = 0;
$sum = 0;
for( ; $pivot_b < strlen($pi); ) {
if($sum < $n) {
$sum += $pi[$pivot_b++];
} elseif ($sum > $n) {
$sum -= $pi[$pivot_a++];
} else {
print('Solution found from digit '.$pivot_a.' to '.$pivot_b.'.');
exit;
}
}
print('No match was found.');
Method 2
This implementation uses one pivot only, from which it starts summing up the digits. If the sum happens to be greater than the desired value, it resets the sum to zero, shifts the pivot one position and starts the summing again.
$pi = "314159265358979323846264338327950288419716939";
$n = 30;
// Let's sum up all the elements from $pivot until we get the exact sum or a
// number greater than that. In the latter case, shift the $pivot one place.
$pivot = 0;
$sum = 0;
for($k=0 ; $sum != $n && $k < strlen($pi) ; $k++) {
$sum += $pi[$k];
print($pi[$k]);
if($sum > $n) {
print(' = '.$sum.' fail, k='.($pivot+1).PHP_EOL);
$sum = 0;
$k = $pivot++;
} elseif($sum < $n) {
print("+");
}
}
print(' = '.$n.' found from digit '.$pivot.' to '.$k.'.');
The implementation is not very effective but tries to explain the steps. It prints
3+1+4+1+5+9+2+6 = 31 fail, k=1
1+4+1+5+9+2+6+5 = 33 fail, k=2
4+1+5+9+2+6+5 = 32 fail, k=3
1+5+9+2+6+5+3 = 31 fail, k=4
5+9+2+6+5+3 = 30 found from digit 4 to 10.
Here's another approach. It builds an array of sums along the way and, on every iteration, attempts to add the current digit to the previous sums, and so on, while always only keeping the sums that are still relevant (< target).
The function either returns:
an array of 2 values representing the 0-based index interval within the digits,
or null if it couldn't find the target sum
Code:
function findFirstSumOccurrenceIndexes(string $digits, int $targetSum): ?array
{
$sums = [];
for ($pos = 0, $length = strlen($digits); $pos < $length; $pos++) {
$digit = (int)$digits[$pos];
if ($digit === $targetSum) {
return [$pos, $pos];
}
foreach ($sums as $startPos => $sum) {
if ($sum + $digit === $targetSum) {
return [$startPos, $pos];
}
if ($sum + $digit < $targetSum) {
$sums[$startPos] += $digit;
}
else {
unset($sums[$startPos]);
}
}
$sums[] = $digit;
}
return null;
}
Demo: https://3v4l.org/9t3vf

If number is not multiple of 6 find remainder in PHP

Okay i will have a number from my database. This could be 3, 15 , 138 etc. Basically any number.
Now if the number is not a multiple of 6 i want to find out how many more until it becomes a multiple of 6.
So for instance if my number is 4, i want it to say you need 2 more to reach a multiple of 6.
How can i achieve this? Also when giving an answer could you please explain how the formula works.
I have tried this which someone suggested
$number = 4;
if($number % 6 != 0) {
echo $number += 6 - ($number % 6);
}
But this just prints 6 out
Using += modifies $number by the value returned
echo $number += 6 - ($number % 6);
Results in: 4 += 6 - 4 or $number = 6
Should be
echo $number = 6 - ($number % 6);
Results in: $number = 6 - 4 or $number = 2
Your numbers are backwards:
$number = 4;
if(6 % $number != 0) {
echo (6 % $number) - 6;
}
https://3v4l.org/1V5Rb

How to Round Decimal Number in PHP

What I want to achieve is like this :
10678.62 then round to 10679 <br/>
10678.67 then round to 10679 <br/>
10678.46 then round to 10678.5 <br/>
10678.43 then 10678.43
what php function I can used to get it work like above example? because as far as I tried (round, ceil, etc) it must be set how much number behind comma.
logic
if .x1 x2 x3 xn (x1 is first number behind comma and so on)
if x1 >= 5 then round to integer (ex: 10678.62 then result 10679)
if x1 < 5 and x2 >=5 then x1 + 1 (ex: 10678.46 then result 10678.5)
if x1 < 5 and xz < 5 then nothing rounded 9 (ex 10678.43 then 10678.43)
so basicly we must see if x1 >= 5 then rounded integer but if x1 < 5 check next number if x2 >= 5 then round to 1 number behind comma if not then round to two number behind comma
Any solution will be very appreciated.
regards
You just need to say how many digits it needs to round:
$number = 10678.48;
$pos = strpos($number, '.');
$len = strlen($number);
$digits = substr($number, $pos+1, $len-$pos);
if($digits<50){
echo round($number,1);
}else{
echo round($number,0);
}
If i understood your question correctly, this should work for you:
function formatRound($number) {
$decimal = (string) fmod($number, 1);
if ($decimal[2] >= 5) {
return round($number);
}
if ($decimal[2] < 5 && $decimal[3] >= 5) {
return round($number, 1);
}
if ($decimal[2] < 5 && $decimal[3] < 5) {
return $number;
}
}
var_dump(formatRound(10678.62));
var_dump(formatRound(10678.67));
var_dump(formatRound(10678.46));
var_dump(formatRound(10678.43));

how can this algorithm be optimized

I am trying to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20 and here is the code:
$num = 2520;
$x = 1;
while($x < 21){
if($num % $x == 0){
$x++;
}else{
$num += 20;
$x = 1;
}
}
echo $num;
it gives a proper output in less than 1 minute. Is this execution time bad in the professional world? any way to optimize this?
P.S. I started from 2520 because it is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
Suggestion: find the primes of all integers in [1,20].
Eg, we have the primes {2,3,5,7,11,13,17,19}. So, if the solution is divisible
by all integers in [1,20], then surely it is divisible by each element in this
list of primes. So, at a minimum, our solution is >= 2*3*5*7*11*13*17*19, right?
Now the problem is how clever we can be about constructing candidate solutions
larger than that number. Well, let's first see how much of the solution is done...
Is 2*3*5*7*11*13*17*19 divisible by 4? No. So, let's multiple by 2 to get
2*2*3*5*7*11*13*17*19, which surely is divisible by 2*2...
Is 2*2*3*5*7*11*13*17*19 divisible by 6? Yes.
Is 2*2*3*5*7*11*13*17*19 divisible by 8? ....
You get the picture. While I am not sure, I believe this approach will
result in the right answer -- ie, the smallest integer divisible by each
integer in [1,20].
use modified Sieve of Eratosthenes for massive speed up
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
but do not use array instead iterate just indexes
something like
1.int ix[20]={1,2,3,4,...20};
2.now write a loop
where you will increment all ix[i]+=i+1; where i=<0;18>
while ix[i]>=ix[19]
if at the end all ix[i] are the same then its contens is the result so stop/return whatever
3.if not then increment also ix[19]+=20;
and continue with the loop on bullet 2
[notes]
can be easily changed for searing for divisible by any numbers not just divisible by 1..20
This sounds like Project Euler's Problem 5.
What we are finding here is the least common multiple of numbers 1 to 20. You can find this with help form the greatest common divisor:
function gcd($a, $b) {
if ($a == 0) return $b;
return gcd($b % $a, $a);
}
function lcm($a, $b) {
return $a * $b / gcd($a, $b);
}
function smallestDiv($n) {
$div = 1;
for ($i = 1; $i <= $n; $i++) {
$div = lcm($div, $i);
}
return $div;
}
echo smallestDiv(20);
Pardon my PHP. It's been a while since I have written any.
Note that we can also find the answer via prime factorization of each of the numbers and looking for the largest exponents as described here and in Kode Charlie's answer:
2 = 2
3 = 3
4 = 2²
5 = 5
6 = 2 × 3
7 = 7
8 = 2³
9 = 3²
10 = 2 × 5
11 = 11
12 = 2² × 3
13 = 13
14 = 2 × 7
15 = 3 × 5
16 = 2⁴
17 = 17
18 = 2 × 3²
19 = 19
20 = 2² × 5
lcm(1,2,…20) = 2⁴ × 3² × 5 × 7 × 11 × 13 × 17 × 19 = 232,792,560

Replace duplicate values in array with new randomly generated values

I have below a function (from a previous question that went unanswered) that creates an array with n amount of values. The sum of the array is equal to $max.
function randomDistinctPartition($n, $max) {
$partition= array();
for ($i = 1; $i < $n; $i++) {
$maxSingleNumber = $max - $n;
$partition[] = $number = rand(1, $maxSingleNumber);
$max -= $number;
}
$partition[] = $max;
return $partition;
}
For example: If I set $n = 4 and $max = 30. Then I should get the following.
array(5, 7, 10, 8);
However, this function does not take into account duplicates and 0s. What I would like - and have been trying to accomplish - is to generate an array with unique numbers that add up to my predetermined variable $max. No Duplicate numbers and No 0 and/or negative integers.
Ok, this problem actually revolves around linear sequences. With a minimum value of 1 consider the sequence:
f(n) = 1 + 2 + ... + n - 1 + n
The sum of such a sequence is equal to:
f(n) = n * (n + 1) / 2
so for n = 4, as an example, the sum is 10. That means if you're selecting 4 different numbers the minimum total with no zeroes and no negatives is 10. Now go in reverse: if you have a total of 10 and 4 numbers then there is only one combination of (1,2,3,4).
So first you need to check if your total is at least as high as this lower bound. If it is less there is no combination. If it is equal, there is precisely one combination. If it is higher it gets more complicated.
Now imagine your constraints are a total of 12 with 4 numbers. We've established that f(4) = 10. But what if the first (lowest) number is 2?
2 + 3 + 4 + 5 = 14
So the first number can't be higher than 1. You know your first number. Now you generate a sequence of 3 numbers with a total of 11 (being 12 - 1).
1 + 2 + 3 = 6
2 + 3 + 4 = 9
3 + 4 + 5 = 12
The second number has to be 2 because it can't be one. It can't be 3 because the minimum sum of three numbers starting with 3 is 12 and we have to add to 11.
Now we find two numbers that add up to 9 (12 - 1 - 2) with 3 being the lowest possible.
3 + 4 = 7
4 + 5 = 9
The third number can be 3 or 4. With the third number found the last is fixed. The two possible combinations are:
1, 2, 3, 6
1, 2, 4, 5
You can turn this into a general algorithm. Consider this recursive implementation:
$all = all_sequences(14, 4);
echo "\nAll sequences:\n\n";
foreach ($all as $arr) {
echo implode(', ', $arr) . "\n";
}
function all_sequences($total, $num, $start = 1) {
if ($num == 1) {
return array($total);
}
$max = lowest_maximum($start, $num);
$limit = (int)(($total - $max) / $num) + $start;
$ret = array();
if ($num == 2) {
for ($i = $start; $i <= $limit; $i++) {
$ret[] = array($i, $total - $i);
}
} else {
for ($i = $start; $i <= $limit; $i++) {
$sub = all_sequences($total - $i, $num - 1, $i + 1);
foreach ($sub as $arr) {
array_unshift($arr, $i);
$ret[] = $arr;
}
}
}
return $ret;
}
function lowest_maximum($start, $num) {
return sum_linear($num) + ($start - 1) * $num;
}
function sum_linear($num) {
return ($num + 1) * $num / 2;
}
Output:
All sequences:
1, 2, 3, 8
1, 2, 4, 7
1, 2, 5, 6
1, 3, 4, 6
2, 3, 4, 5
One implementation of this would be to get all the sequences and select one at random. This has the advantage of equally weighting all possible combinations, which may or may not be useful or necessary to what you're doing.
That will become unwieldy with large totals or large numbers of elements, in which case the above algorithm can be modified to return a random element in the range from $start to $limit instead of every value.
I would use 'area under triangle' formula... like cletus(!?)
Im really gonna have to start paying more attention to things...
Anyway, i think this solution is pretty elegant now, it applies the desired minimum spacing between all elements, evenly, scales the gaps (distribution) evenly to maintain the original sum and does the job non-recursively (except for the sort):
Given an array a() of random numbers of length n
Generate a sort index s()
and work on the sorted intervals a(s(0))-a(s(1)), a(s(1))-a(s(2)) etc
increase each interval by the
desired minimum separation size eg 1
(this necessarily warps their
'randomness')
decrease each interval by a factor
calculated to restore the series sum
to what it is without the added
spacing.
If we add 1 to each of a series we increase the series sum by 1 * len
1 added to each of series intervals increases sum by:
len*(len+1)/2 //( ?pascal's triangle )
Draft code:
$series($length); //the input sequence
$seriesum=sum($series); //its sum
$minsepa=1; //minimum separation
$sorti=sort_index_of($series) //sorted index - php haz function?
$sepsum=$minsepa*($length*($length+1))/2;
//sum of extra separation
$unsepfactor100=($seriesum*100)/($seriesum+sepsum);
//scale factor for original separation to maintain size
//(*100~ for integer arithmetic)
$px=series($sorti(0)); //for loop needs the value of prev serie
for($x=1 ; $x < length; $x++)
{ $tx=$series($sorti($x)); //val of serie to
$series($sorti($x))= ($minsepa*$x) //adjust relative to prev
+ $px
+ (($tx-$px)*$unsepfactor100)/100;
$px=$tx; //store for next iteration
}
all intervals are reduced by a
constant (non-random-warping-factor)
separation can be set to values other
than one
implementantions need to be carefuly
tweaked (i usualy test&'calibrate')
to accomodate rounding errors.
Probably scale everything up by ~15
then back down after. Intervals should survive if done right.
After sort index is generated, shuffle the order of indexes to duplicate values to avoid runs in the sequence of collided series.
( or just shuffle final output if order never mattered )
Shuffle indexes of dupes:
for($x=1; $x<$len; $x++)
{ if ($series($srt($x))==$series($srt($x-1)))
{ if( random(0,1) )
{ $sw= $srt($x);
$srt($x)= $srt($x-1);
$srt($x-1)= $sw;
} } }
A kind of minimal disturbance can be done to a 'random sequence' by just parting dupes by the minimum required, rather than moving them more than minimum -some 'random' amount that was sought by the question.
The code here separates every element by the min separation, whether duplicate or not, that should be kindof evenhanded, but overdone maybe. The code could be modified to only separate the dupes by looking through the series(sorti(n0:n1..len)) for them and calculating sepsum as +=minsep*(len-n) for each dupe. Then the adjustment loop just has to test again for dupe before applying adjustment.

Categories