Modulus PHP Problem - php

I have a problem, I am trying to calculate what the lowest prime is of a number but I do not understand the result that PHP is giving me.
If I have this number
$number = 600851475143;
Then I modulus it:
$primes = array( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
foreach($primes as $key=>$value) {
if($number % $value == 0 ) {echo $value; break; }
}
Why is it that $value = 3? If $value = 3, that means that 600851475143 / 3 should be an integer, but its not. So I do not understand why that if() evaluates to true?

See this bug listing here
% does not not work for numbers over 2^31 (32-bit) or 2^63 (64-bit). Use BCMOD instead.

Using PHP 5.2.8, it fails as described in the question. It seems like the modulo operator doesn't work on big integers. You should consider using bc_mod (modulus with arbitrary precision):
<?php
$number = 600851475143;
$primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
foreach($primes as $key=>$value)
{
if($number % $value == 0 ) {echo $value."<br/>"; }
if(bcmod($number, $value) == 0) {echo "bcmod ".$value."<br/>"; }
}
?>
The above code prints:
3
29
bcmod 71

I might be misunderstanding it, but modulo gives you the rest of a division, so e.g. 600851475143 / 3 is 200283825047 rest 2 and this is what it gives you back.

Related

How to show more than 1 data in php array after doing min and max

I'm doing my homework and I can't find the solution to displaying more than one result in array after doing min and max syntax
my teacher said that I should use min and max to show more than 1 result
$temperatures = [78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76, 73, 68, 62, 73, 72, 65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 73];
$max = max($temperatures);
$min = min($temperatures);
The final result should be:
average of the temperatures : 70.6
the five lowest temperature lists : 60, 62, 63, 63, 64
the five highest temperature lists : 76, 78, 79, 81, 85
My two cents on it:
$temperatures = [78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76, 73, 68, 62, 73, 72, 65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 73];
# simply sum the elements then divide by count
$avg = (array_sum($temperatures) / count($temperatures));
# sort arr low -> high
sort($temperatures);
# make els unique
$temperatures = array_unique($temperatures);
$min = array_slice($temperatures, 0, 5); # get first 5 in array
$max = array_slice($temperatures, -5); # get last 5 in array
echo '<pre>'. print_r($avg, 1) .'</pre>';
echo '<pre>'. print_r($min, 1) .'</pre>';
echo '<pre>'. print_r($max, 1) .'</pre>';

How to calculate random numerical sequency number?

I'm trying to build the following numerical series 1, 1, 2, 3, 4, 5, 5, 6 until 100 ( It is a homework) . I have to do this using php code but I cannot get it , I've read the fibonacci method but the numerical series numbers are different.
<?php
$a=1;
$serie="1";
for ($i=1;$i<=100;$i++)
{
if($i%5==0)
{
$serie=$serie.",$i,$i";
}
else
{
$serie=$serie.",$i";
}
}
print $serie;
?>
As #tim pointed out in the comment, the solution is not printing a sequence from 1 to 100 where only numbers that are MOD 5 = 0 are duplicated. That way you do not get the repetition on 1.
From your homework question (which is not really clear in my opinion) I presume you want something like this:
<?php
for ($i = 0; $i < 100; $i++) {
echo $i+1 . ", ";
if ($i % 4 == 0) {
echo $i+1 . ", ";
}
}
?>
It prints:
1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 13, 14, 15, 16, 17, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25, 25, 26, 27, 28, 29, 29, 30, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 49, 50, 51, 52, 53, 53, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 73, 73, 74, 75, 76, 77, 77, 78, 79, 80, 81, 81, 82, 83, 84, 85, 85, 86, 87, 88, 89, 89, 90, 91, 92, 93, 93, 94, 95, 96, 97, 97, 98, 99, 100,
The logic is to print the index increased by 1 each iteration, and print it again if the result of the modulus operation index % 4 is equal to 0.

Addition to values in a foreach-loop

So I have an array.
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
What I have to do is use a foreach-loop to sum each item with 20 and then place them in a new Array, $newArray. This is what I've come up with so far.
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as &$value) {
$newArray = $value + 20;
}
But it doesn't seem to be working, as I receive the answer 22 instead of the array with the sum of the numbers. I know I have to echo it out, but I have to do that later in the exercise. I appreciate the help.
Your question basically works. Replace $newArray with $value as so:
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as &$value) {
$value += 20;
}
Then if you need it in a new array use add the following line afterwards:
$newArray = $numbers1;
Since you are passing $value by reference you can use the $value += 20 line.
If you don't want the pointless array reassignment you can do the following:
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as value) {
$newArray[] = $value + 20;
}
General solution, not suitable for this exact question
Use array_map instead(for better codestyle):
$newArray = array_map(function ($x) {
return $x + 20;
}, [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2]);
This will assign the function to each entry and thus will increase each value by 20.

PHP Recursive Function top list

I want make recursive function
when i search any id the id introid will show and introid will show their introid [Urdu Guide]
if i search id 45
will show result like
22
11
5
2
1
<?php
function searchID($id)
{
if ($id>1) {print intval($id/2)."\n"; searchID(intval($id/2));} else return 1;
}
searchID(45);
?>
Assuming you may have tried something and not successful with the recursion approach ... here is the solution!
<?php
$introid = array(0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25);
$id = range(1, count($introid));
$associative_array= array_combine($id, $introid);
function lets_recurse($id, $associative_array)
{
if($id==1 || $id==0)
{
echo '';
}
else if(isset($associative_array[$id]))
{
echo $associative_array[$id].' ';
lets_recurse($associative_array[$id], $associative_array);
}
else
{
echo '';
}
}
lets_recurse(45, $associative_array);
?>

How to limit an array and split it into more arrays? PHP

I would like to do this...
I got this array with 50 elements
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50);
and I would like to add the first 20 elements into another array, 20 should be the limit, if it is greater than 20 add the next 20 into another array and do this operation to only get array with 20 elements
I tried with this
$number = count($data);
$pieces = array_chunk($data, ceil($number / 2));
And I get only two sub-arrays from that, I'm lost, I need some ideas about how to achieve this, thanks.
That's because you're only trying to get 2 subarrays, since you're taking the total number and dividing it by 2. You're on the right track, you just need to specify the number of elements you want in each subarray:
$pieces = array_chunk($data, 20);

Categories