I have a PHP problem.
I need to write a number from a sets of digits 0-9. Each set has 10 digits, each digit once.
I need to count the number of sets that I have to use to write the number.
For example number 10 is written from one set, but number 300 uses 2 sets because it has two zeros.
But, the problem is that 6 and 9 are considered the same. They can be rotated by 180 degrees.
Number 266 will be using one set, 369 also is using one set, but 5666 is using 2 sets.
I would be very grateful if you could somehow help me.
Here is how I have started and stuck up, have no more clue how to loop through it. Tried many things, nothing successful.
<?php
function countSet($num) {
$array = str_split($num);
$statarr = [0,1,2,3,4,5,6,7,8,9];
$a1 = $array; $a2 = $statarr;
$result = array_intersect($a1,$a2);
$count = array_count_values($result); }
?>
If you just need to know how many sets you need for a number, you can solve by counting the numbers. For the case of the 9, just replace every 9 by a 6 and divide the number of 6 by two. Something like this (sorry if there's any syntax error, I'm on mobile):
function countSet($input) {
// Convert the input into a string and replace every 9 by a 6. Then convert it to array
$numbers = str_split(str_replace("9", "6", strval($input)));
// Count occurrences for each number
$usedNumbers = array_count_values($numbers);
// If we have a 6, must divide it by 2 (6 represents 6 and 9
if (array_key_exists("6", $usedNumbers)) {
$usedNumbers["6"] = ceil($usedNumbers["6"] / 2);
}
// Now we just need to know the max number of occurrences for a single number as our result.
return max($usedNumbers);
}
See an online demo here
Related
This question already has answers here:
PHP loop X amount of times
(10 answers)
Random Numbers without duplication using an array in PHP
(1 answer)
Closed 4 years ago.
How can I create a loop or limit for random number generation? I need 30 random numbers from (0,100).
I know I can use rand(0,100), for general generation of numbers. But I need to make this happen 30 times before I can sort out the data.
Repetition of numbers is NO issue, but I also need to sort the values into x<50 and x>50. Any idea how I can pull numbers from the array into 2 separate groups once generated?
Try this:
$lessFifty = array(); // Array to hold less than fifty numbers
$moreFifty = array(); // Array to hold more than fifty numbers
for ($i = 1; $i<=30; $i++) {
$number = rand(0, 100); // Variable $number holds random number
// Conditional statements
if ($number < 50 ) { // Check if value is less than fifty
$lessFifty[] = $number; // Add number to this array
} else { // Check if value is greater than fifty
$moreFifty[] = $number; // Add number to this array
}
}
// Display numbers in arrays
print_r($lessFifty);
print_r($moreFifty);
The for loop will will run the rand() function 30 times and insert each random number generated into the array $randomNum.
You can also use the while or do while loop to do the same action. It is up to you.
I have an array and I am getting its length using sizeof(). In the size of the array I want to get how many 10 there are in that number. I used % in getting my number like:
$arraysize = 13;//sizeof($array)
$numberoften = $arraysize % 10 // getting 3
From here I know that I am wrong. What i want to get is 2. Because I want to get every 10. Like if length is 9 I want to get 1. If length is 31 I want to get 4.
What is the correct function of way to get my desired output?
What you're after is ceil()
Returns the next highest integer value by rounding up value if necessary.
$numberoften = ceil($arraysize / 10);
There's also floor() if you want to round the number down to the nearest integer. This would seem to fit better with your question...
I want to get how many 10 there are in that number
Use ceil() function
$arraysize = 13;//sizeof($array);
$numberoften =ceil( $arraysize/10);
Or
Use round() function
$arraysize = 13;//sizeof($array);
$numberoften =round( $arraysize/10);
Not quite sure what to set this title as, or what to even search for. So I'll just ask the question and hope I don't get too many downvotes.
I'm trying to find the easiest way to find the highest possible number based on two fixed numbers.
For example:
The most I can multiply by is, say, 18 (first number). But not going over the resulted number, say 100 (second number).
2 x 18 = 36
5 x 18 = 90
But if the first number is a higher number, the second number would need to be less than 18, like so:
11 x 9 = 99
16 x 6 = 96
Here I would go with 11, because even though the second number is only 9, the outcome is the highest. The second number could be anything as long as it's 18 or lower. The first number can be anything, as long as the answer remains below 100. Get what I mean?
So my question is, how would write this in php without having to use switches, if/then statements, or a bunch of loops? Is there some math operator I don't know about that handles this sort of thing?
Thanks.
Edit:
The code that I use now is:
function doMath($cost, $max, $multiplier) {
do {
$temp = $cost * $multiplier;
if ($temp > $max) { --$multiplier; }
} while ($temp > $max);
return array($cost, $temp, $multiplier);
}
If we look at the 11 * 9 = 99 example,
$result = doMath(11, 100, 18);
Would return,
$cost = 11, $temp = 99, $multiplier = 9
Was hoping there was an easier way so that I wouldn't need to use a loop, being as how there are a lot of numbers I need to check.
If I understood you right, you are looking for the floor function, combining it with the min function.
Both a bigger number c and a smaller number a are part of the problem, and you want to find a number b in the range [0, m] such that a * b is maximal while staying smaller (strictly) than c.
In your example, 100/18 = 5.55555, so that means that 18*5 is smaller than 100, and 18*6 is bigger than 100.
Since floor gets you the integral part of a floating point number, $b = floor($c/$a) does what you want. When a divides c (that is, c/a is an integer already), you get a * b == c.
Now b may be outside of [0,m] so we want to take the smallest of b and m :
if b is bigger than m, we are limited by m,
and if m is bigger than b, we are limited by a * b <= c.
So in the end, your function should be :
function doMath($cost, $max, $multiplier)
{
$div = min($multiplier, floor($max/$cost));
return array($cost, $div * $cost, $div);
}
http://www.republicof3.com/how-to-generate-unique-promotion-discount-codes-in-php/
I have found a function to generate unique promotion code in PHP
But there is a big problem, if the number of codes to be generated more then the number of remain combination(all combination-length of exclude_codes_array), the function will become infinite loop.
For example: If the $characters = "0A"; the combination are "00", "0A", "A0", "AA" = 4, if the $no_of_codes >4, the loop is cannot stopped.
So I want to check the the number of codes and all remaining combination of codes before the loop, how to calculate the combination of a string?
As per what you have calculated for A0.
AA, 00 , A0, 0A
Generalizing and using basic permutation and combination it should be ,
2^{strlen($str)};
Discrete Math
available chars = 2 ("A", "0")
available spaces = 2 ("**")
chasr^spaces = 4
available chars = 2 ("A", "0")
available spaces = 3 ("***")
chars^spaces = 8
available chars = 3 ("A", "B", "0")
available spaces = 2 ("**")
chars^spaces = 9
What would be a good way to generate 7 unique random numbers between 1 and 10.
I can't have any duplicates.
I could write a chunk of PHP to do this (using rand() and pushing used numbers onto an array) but there must be a quick way to do it.
any advice would be great.
Create an array from 1 to 10 (range).
Put it in random order
(shuffle).
Select 7 items from the array (array_slice)
Populate an array with ten elements (the numbers one through ten), shuffle the array, and remove the first (or last) three elements.
Simple one-liner:
print_r(array_rand(array_fill(1, 10, true), 7));
Check out the comments in the php manual, there are several solutions for this.
An easy one is this one:
$min = 1;
$max = 10;
$total = 7;
$rand = array();
while (count($rand) < $total ) {
$r = mt_rand($min,$max);
if (!in_array($r,$rand)) $rand[] = $r;
}
Whole numbers? Well, if you want 7 out of 10 then you more efficiently DON'T want 3 out of 10.
Feel free to use any of the other responses but instead of creating 7 numbers start with 10 and eliminate 3. That will tend to speed things up by more than double.
The "shuffle" method has a MAJOR FALW. When the numbers are big, shuffle 3 billion indexs will instantly CAUSE 500 error. Here comes a best solution for really big numbers.
function getRandomNumbers($min, $max, $total) {
$temp_arr = array();
while(sizeof($temp_arr) < $total) $temp_arr[rand($min, $max)] = true;
return $temp_arr;
}
Say I want to get 10 unique random numbers from 1 billion to 4 billion.
$random_numbers = getRandomNumbers(1000000000,4000000000,10);
PS: Execution time: 0.027 microseconds