skip to Main Content

Integer Properties

  1. Integer is ended with 0 or 5

Example: 64454542420, 83186482697426868635

  1. Integer is very big.

Aim:

  1. Want to get last digit of the integer. So desired output is 0 or 5

  2. Can be used gmp for long integer

    $int = gmp_init("1166463664644444353335343545"); // Example input as a GMP integer

  3. Output and execution time is to be same as

    echo $in%10; 
    

Limitation

  1. I can not use /, %, strlen or any other operator

  2. Only +, -, *, >, <, = and for loop can be used

Code that I am trying

<?php

$int = 1166463664644444353335343545; // Example input

// Initialize a variable to hold the last digit
$lastDigit = $int;

// Keep subtracting 10 until we get a single digit
while ($lastDigit >= 10) {
    $lastDigit = $lastDigit - 10;
}

// Output the last digit
echo $lastDigit;

?>

Problem

  1. Extremely slow due to big loop.

Bounty

  1. Your time and thinking is valuable to me as it is complex finding. So, I will give $300 to the first person who can give perfect solution of it. Payment method – Crypto

Clue

My clue suppose int = 115, now add 5 = 120 now sub 10 = 110 for ending 5. if int = 110 now add 5 = 115, sub 10 = 105 ……. like this way….

Or

Try to eliminate digit from the left making 0. Finally it may possible to get 5 or 0. Think about adding or substucting 10, 100, 1000, 10000, 100000 ete using loop

2

Answers


  1. For the question asked as it is now – there is no reason to not simply chop the last character of the string:

    $str = '1166463664644444353335343545';
    $result = (int)$str[-1];
    
    Login or Signup to reply.
  2. preg_match('/(d)$/', $int, $matches);
    return $matches[1];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search