Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP Math


ghost's Avatar
0 0

I'm working on some math functions in php. I did these three about a month ago. I'm not too good in math, so if there's any errors, please let me know. And, please post some of your own.

function exponents($ammt, $power){
 $counter = 0;
 while ($counter != $power){
   $number = $ammt * $ammt;
   if ($realnumber){
   $realnumber = "$realnumber * $number";
   }else{
   $realnumber = $number;
   }
   $counter++;
  }
 $realnumber = 1 * $realnumber;
 print "$ammt to the $power power = $realnumber.";
}

function prime($ammt){
 for ( $counter = 1; $counter <= $ammt; $counter += 1) {
  $number = $ammt/$counter;
   if ($counter != 1 && $counter != $ammt){
    if (is_int($number)){
     $composite = 1;
    }
   }
  }
 if ($composite){
  print "$ammt is a composite number.";
 }else{
  print "$ammt is a prime number.";
 }
}

function oddeven($number){
 $number = $number/2;
   if (is_int($number)){
   print "$ammt is an even number.";
  }else{
   print "$ammt is an odd number.";
  }
}

ghost's Avatar
0 0

I'd personally use modulus but thats just me


ghost's Avatar
0 0

i don't know english well, so i can't post my own as you requested :D

{
    $counter = 0;
    $number  = 1;
    if ( $power >= 1 ) {
        
        do {
            $number = $number * $ammt;
            $counter++;
        } while ( $counter != $power );
    } elseif ( $power <= -1 ) {
        do {
            $number = $number * 1/$ammt;
            $counter--;
        } while ( $counter != $power );
    }

   
   echo $ammt .'to the '.$power.' power = '.$number ;

}

function prime($ammt)
{
    for ( $counter = 1; $counter <= $ammt; $counter++ ) {
        if ($counter != 1 && $counter != $ammt){
            if ($ammt%$counter){
                $composite = 1;
            }
        }
    }
    if ($composite){
        echo $ammt.' is a composite number.';
    }else{
        echo $ammt.' is a prime number.';
    }
}

function oddeven($number)
{
    if ( ! $number%2 ) {
        echo $number.' is an even number.';
    } else {
        echo $number.' is an odd number.';
    }
}

//oddeven(6);
//prime(11);
//exponents(5,-3); you can use pow(5,-3);```

ghost's Avatar
0 0

o thanks. I didn't know there was already a function for powers…