Is there a faster/better way to leettime?
I found leettime via twitter, and it's novel enough for me to want to mess around with it/share it with you guys. To my dismay this version doesn't do seconds. It also seems like there might be a more efficient way to generate the time, what do you think?
Leet Time
Now, how is this leettime cal开发者_JAVA技巧culated?
For a given humantime (e.g. 11:15 am) the value of the leettime corresponds to the number of times you have to add up 13 hours 37 minutes in order to exactly reach this time. Whenever the result overflows during the calculation (i.e. the obtained time exceeds 23:59 o'clock) you subtract 24 hours in order to stay on the clock.
Example:
The leettime of 00:00 is 0, the leettime of 13:37 is 1 and leettime 2 corresponds to 03:14 am (because 13:37 plus 13 hours 37 minutes is 03:14 o'clock).
Is there a unique leettime for every humantime during the day?
Yes! There are exactly 24*60=1440 different leettimes, each corresponds to exactly one minute in the day.
Add whatever else you think would be cool, I'm sure this guy would love it.
I put the PHP version up figuring it'd be the most accessible and portable. The other versions are available here.
<?php
/**
* Converts standard time to leettime.
*
* @param int h the hour in standard time
* @param int m the minute in standard time
*
* @return int the leettime or -1 if the input
* parameters are invalid
*/
function TimeToLeet($h, $m){
if(!is_numeric($h) || !is_numeric($m) ||
$h > 23 || $h < 0 || $m > 59 || $m < 0)
return -1;
$curm = 0;
$curh = 0;
$i = 0;
while ($curm != $m || ($curh % 24) != $h){
if($curm < 23){
$curh += 13;
$curm += 37;
} else {
$curh += 14;
$curm -= 23;
}
++$i;
}
return $i;
}
/**
* Converts leettime to standard time.
*
* @param int leet the time in leettime-format in the
* range from 0 - 1439
*
* @return var an int-Array with the hours at position 0
* and minutes at position 1 (-1 if the input parameter
* is not in range)
*/
function LeetToTime($leet){
if($leet > 1439 || $leet < 0) return array(-1, -1);
$m = $leet * 37;
$h = ($leet * 13) + ($m / 60);
return array($h % 24, $m % 60);
}
// Demonstrate usage
$h = (int)date("G");
$m = (int)date("i");
echo date("H:i")." = ".TimeToLeet($h,$m);
echo "
";
$leettime = 999;
$result = LeetToTime($leettime);
if($result[0] == -1) echo "error";
else {
$h = $result[0];
$m = $result[1];
echo $leettime." = ".($h>9?$h:"0".$h) .
":".($m>9?$m:"0".$m);
}
?>
For a faster conversion from HH:MM to Leet, try Modular Arithmetic:
First, convert 13:37 to a number mod 1440; specifically, 13*60+37 = 817
Find the inverse of 817 mod 1440. Result is 913 (stolen from Wolfram Alpha). 913 has the property that 913*817 == 1 mod 1440.
Alternatively, if you wanted to use 13 minutes, 37 seconds; you would work mod 86400, and 817^-1 == 67153.
Convert your target time to a number mod 1440; then multiply by 913 mod 1440 to get its corresponding leet time. To convert back, multiply by 817.
Example:
Going from leet time to normal time (accuracy to the minute):
someLeetTime = 1337
//where 817 = 13*60 + 37 = 13:37 in minutes
timeInMinutes = someLeetTime * 817 mod 1440 = 809
hours = timeInMinutes / 60 = 13
minutes = timeInMinutes % 60 = 29
Going from normal time to leet time (accuracy to the minute):
hours = 13
minutes = 29
timeInMinutes = hours * 60 + minutes = 809
//913 = 817^-1 mod 1440
someLeetTime = timeInMinutes * 913 mod 1440 = 1337
13:29 is 1337 leet time
And, one possible implementation for seconds, where in 13:37 is treated as 13 minutes, 37 seconds. (Technically violates the standard, but we are l33t, and can do that)
Going from leet time to normal time (accuracy to the second):
someLeetTime = 1337
//where 817 = 13*60 + 37 = 13:37 in seconds
timeInSeconds = someLeetTime * 817 mod 86400 = 55529
hours = timeInSeconds / 3600 = 15
minutes = (timeInSeconds % 3600) / 60 = 25
seconds = timeInSeconds % 60 = 29
1337 in the new system is 15:25:29
Going from normal time to leet time (accuracy to the second):
hours = 15
minutes = 25
seconds = 29
timeInSeconds = hours * 3600 + minutes*60 + seconds = 55529
//67153 = 817^-1 mod 86400
someLeetTime = timeInSeconds * 67153 mod 86400 = 1337
15:25:29 is 1337 leet time
Thank you Crypto, and Modular Arithmetic.
For the furtherance of this great cause, I have built a clock! Well... 6 actually.
精彩评论