Integer Properties
- Integer is ended with 0 or 5
Example: 64454542420, 83186482697426868635
- Integer is very big.
Aim:
-
Want to get last digit of the integer. So desired output is 0 or 5
-
Can be used gmp for long integer
$int = gmp_init("1166463664644444353335343545"); // Example input as a GMP integer
-
Output and execution time is to be same as
echo $in%10;
Limitation
-
I can not use /, %, strlen or any other operator
-
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
- Extremely slow due to big loop.
Bounty
- 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
For the question asked as it is now – there is no reason to not simply chop the last character of the string: