I am trying to turn numbers into letters to create references for rows.
I have:
public static function references($idx) {
$str = '';
$i = ceil($idx/25);
if(65+$idx > 90) {
} else {
$str = chr(65+$idx);
}
return $chr;
}
But I don't know where to go from here.
Valid outputs would be:
first item: A
28th item: AB
...
Input is an index that comes in from a loop, i.e. 0, 1, 2, 3 etc
I figured it out:
public static function references($n)
{
$r = '';
for ($i = 1; $n >= 0 && $i < 10; $i++) {
$r = chr(0x41 + ($n % pow(26, $i) / pow(26, $i - 1))) . $r;
$n -= pow(26, $i);
}
return $r;
}
Related
I was trying to make function that gives two value. First value was the respite in the radical and the second value is the number that we want to put it in radical.
It's my code:
function radical($respite = 2, $num)
{
$numbers = array();
for ($i = 1; $i < 10; $i++) {
$numbers[] = '0.' . "$i";
}
for ($i = 1; $i < 10; $i++) {
$numbers[] = '1.' . "$i";
}
// I wanted do these loop until to creat numbers from 0.1 to 100,
// but i under stand it's silly work and wrong.
for ($i = 0; $i < sizeof($numbers); $i++) {
if ($respite == 2) {
$hesan = $number["$i"] * $number["$i"];
if ($hesab == $num) {
return $hesab;
}
} elseif ($respite == 3) {
$hesan = $number["$i"] * $number["$i"] * $number["$i"];
if ($hesab == $num) {
return $hesab;
}
}
}
}
I tried to create numbers from 0.1 to 100. and I wanted write if $number[$i] * $number[$i] = $num return the $number, but I saw it's silly work and wrong my means create numbers from 0.1 to 100 by this way.
For radical(2, 9) the output should be 4, because 3 * 3 = 9.
If the first value is 3 radical(3, 8) the output should be 2, because 2 * 2 * 2= 8
Can someone make function to do radical with respite ? or improve my code ?
If you want to do this not as an exercise but for productive use, I suggest this:
function radical($num, $respite = 2)
{
return $num ** (1 / $respite);
}
echo radical(27, 3) . "\n" .
radical(8, 3) . "\n" .
radical(36, 2) . "\n" .
radical(16, 2);
Output:
3
2
6
4
You can see it here
The reason I changed the argument order is that you can't have a required parameter after an optional one.
Hope this is what you are looking for..
Try this code snippet here
function radical($respite = 2, $num)
{
for($x=0;$x<=10;$x+=0.1)
{
$numbers[]=$x;
}
for ($i = 0; $i < sizeof($numbers); $i++)
{
if ($respite == 2)
{
if ((string)($numbers[$i] * $numbers[$i]) == (string)$num)
{
return $numbers[$i];;
}
}
elseif ($respite == 3)
{
if ((string)($numbers[$i] * $numbers[$i]* $numbers[$i]) == (string)$num)
{
return $numbers[$i];
}
}
}
}
print_r(radical(3, 27));//3
print_r(radical(3, 8));//2
print_r(radical(2, 36));//6
print_r(radical(2, 16));//4
I'm not sure what you're going for exactly, but I think this cleans up what you have now at least:
function radical($num, $respite = 2) {
$numbers = array();
for ($i = 1; $i <= 1000; $i++) {
$numbers[] = $i * 0.1;
}
foreach($numbers as $i) {
if ($respite == 2) {
$hesab = $i * $i;
if ($hesab == $num) {
return $i;
}
}
elseif($respite == 3) {
$hesab = $i * $i * $i;
if ($hesab == $num) {
return $i;
}
}
}
return 0;
}
I am working on mathematical problem where the formula is: A[i] * (-2) power of i
where i=0,1,2,3,...
A is an array having values 0 or 1
Input array: [0,1,1,0,0,1,0,1,1,1,0,1,0,1,1]
Output is: 5730
Code
$totalA = 0;
foreach ($A as $i => $a) {
$totalA += $a * pow(-2, $i);
}
This is correct. Now I am looking for its opposite like:
Input is: 5730
Output will be: [0,1,1,0,0,1,0,1,1,1,0,1,0,1,1]
I am not asking for the exact code but looking for some logic from where I should start. I tried to use log() method but that did not return the desired output.
You were not looking for exact code, but I found this problem too interesting. This works:
function sign($n) {
return ($n > 0) - ($n < 0);
}
$target = -2396;
$i = 0;
$currentSum = 0;
// Look for max $i
while (true) {
$val = pow(-2, $i);
$candidate = $currentSum + $val;
if (abs($target) <= abs($candidate)) {
// Found max $i
break;
}
if (abs($target - $candidate) < abs($target - $currentSum)) {
// We are getting closer
$currentSum = $candidate;
}
$i++;
}
$result = [];
for ($j = $i; 0 <= $j; $j--) {
$val = pow(-2, $j);
$border = $val / 4;
if (sign($val) == sign($target) && abs($border) < abs($target)) {
array_unshift($result, 1);
$target -= $val;
} else {
array_unshift($result, 0);
}
}
echo json_encode($result);
First I look for the $i that gets me on or slightly above the $target. When found, I walk down and decide for each bit if it should be in the result.
I want to divide a number into n number of parts like follows
Input : $n = 4;$m =14;
Output should as : array(1=>4,2=>4,3=>3,4=>3);
i.e :
$n $m
1 1+1+1+1
2 1+1+1+1
3 1+1+1
4 1+1+1
Any suggestions or links would help a lot.
Here is one way to do it - this works in phpfiddle:
$n = 4;
$m =14;
$array = distribute($m,$n);
print_r($array);
function distribute($m,$n) {
$div = floor($m / $n);
$mod = fmod($m, $n);
$result = array();
for ($i = 1;$i <= $n;$i++) {
$result[$i] = $div;
}
if ($mod > 0) {
for ($i = 1;$i <= $mod;$i++) {
$result[$i] = $result[$i] + 1;
}
}
return $result;
}
Here is my code:
$n = 300;
$set = 0;
$set2 = 0;
for($i = 1; $i<$n; $i++)
{
for($j = 1; $j <$i; $j++)
{
$qol = $i % $j;
if($qol == 0)
{
$set += $j;
}
}
for($s=1; $s<$set; $s++)
{
$qol2 = $set % $s;
if($s == 0)
{
$set2 += $s;
}
}
if($set2 == $i)
{
echo "$set and $i are amicable numbers</br>";
}
}
I do not know what the heck the problem is!
FYI: 220 and 284 are an example of amicable numbers. The sum of the proper divisors of one number are equal to other number and vice versa (wiki).
I am having troubles following your logic. In your code how would $set2 == $i ever be true? Seems to me that $i would always be greater.
I would do it the following way:
First make a separate function that finds the sums of the proper divisors:
// Function to output sum of proper divisors of $num
function sumDiv($num) {
// Return 0 if $num is 1 or less
if ($num <= 1) {
return 0;
}
$result = 1; // All nums divide by 1
$sqrt = sqrt($num);
// Add divisors to result
for ($i = 2; $i < $sqrt; $i++) {
if ($num % $i == 0) {
$result += $i + $num / $i;
}
}
// If perfect square add squareroot to result
if (floor($sqrt) == $sqrt) {
$result += $sqrt;
}
return $result;
}
Next check each iteration for a match:
$n = 1500;
for ($i = 1; $i < $n; $i++) {
// Get sum of proper devisors of $i, and sum of div. of result.
$currentDivs = sumDiv($i);
$resultDivs = sumDiv($currentDivs);
// Check for a match with sums not equal to each other.
if ($i == $resultDivs && $currentDivs != $resultDivs) {
echo "$i and $currentDivs are amicable numbers<br>";
}
}
Here a functioning phpfiddle.
Warning: Large numbers will take very long to process!
I have this piece of code that loops 1 through 99 and is a formula.
function getExperienceByLevel ($maxLevel)
{
$levels = array ();
$current = 0;
for ($i = 1; $i <= $maxLevel; $i++)
{
$levels[$i - 1] = floor ($current / 4);
$current += floor($i+300*pow(2, ($i/9.75)));
}
return $levels;
}
First you initiate it like so $aLevels = getExperienceByLevel(99); then to see how much EXP you need to get to level 6 you do this echo $aLevels[5]; since it's an array.
Now I'm trying to do reverse. Get Level by EXP.
function getLevelByExp($exp)
{
$aLevels = getExperienceByLevel(99);
for ($i = 1; $i < 100; $i++)
{
if ($exp > $aLevels[$i-1])
{
return $i;
}
}
}
So I try to do this:
$aLevels = getExperienceByLevel(99);
echo getLevelByExp(131);
When called upon getLevelByExp(131); or any number inside, it seems to return a 1 even though it should be 2 since Level 3 is 167 EXP and Level 2 is 80 EXP. Here's a reference image: http://i.imgur.com/gEYgu.png
function getLevelByExp($exp) {
$aLevels = getExperienceByLevel(99);
for ($i = 99; $i >= 1; $i--)
{
if ($exp > $aLevels[$i-1])
{
return $i;
}
}
}
You are returning as soon as $exp > $aLevels[$i-1]. On the first runthrough of your original loop, $aLevels[$i-1] = $aLevels[0] = 0, so it will always return right away for any non-negative $exp value.
You are doing two mistakes: You are indexing your levels array wrong. And you're checking it the wrong way. Use this:
http://codepad.viper-7.com/MGpOUu
function getExperienceByLevel($maxLevel) {
$levels = array ();
$current = 0;
for ($i = 1; $i <= $maxLevel; $i++) {
$levels[$i] = floor ($current / 4);
$current += floor($i+300*pow(2, ($i/9.75)));
}
return $levels;
}
function getLevelByExp($exp) {
$levels = getExperienceByLevel(99);
$current = 0;
foreach($levels as $level => $required) {
if($required>$exp)return $current;
$current = $level;
}
return $current;
}
echo getLevelByExp(131);
// returns 2
change
if ($exp > $aLevels[$i-1])
to
if ($exp > $aLevels[$i-1] && $exp < $aLevels[$i])
checked and it is working
working example http://codepad.viper-7.com/BjmHad
You need the opposite conditional, and to compare to the next level, so you can determine that a given exp level does not fit into any higher level. This code works for me:
if ($exp < $aLevels[$i])
{
return $i;
}
Nice and simple. Try it out: http://codepad.viper-7.com/FrjtHT
I think you should do it like
function getLevelByExp($exp)
{
$aLevels = getExperienceByLevel(99);
for ($i = 1; $i < count($aLevels); $i++)
{
if ($exp >= $aLevels[$i-1] && ($exp - $aLevels[$i-1] < $aLevels[$i] - $aLevels[$i-1]))
{
return $i;
}
}
}
Check out http://www.phpfiddle.org/main/code/paw-08f