tjc19999 Posted May 18, 2011 Share Posted May 18, 2011 I need a way to find numbers 0000-9999 where no number has more than 2 digits in the same place in common. I know PHP but this is a little beyond me, can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/236801-find-if-two-digits-in-same-place-are-the-same/ Share on other sites More sharing options...
requinix Posted May 18, 2011 Share Posted May 18, 2011 Two digits "in the same place"? Quote Link to comment https://forums.phpfreaks.com/topic/236801-find-if-two-digits-in-same-place-are-the-same/#findComment-1217313 Share on other sites More sharing options...
tjc19999 Posted May 19, 2011 Author Share Posted May 19, 2011 Basically if I have the number 1234 I do not want to have anything like 1334 OR 2234, OR 1235 but if i have 1245 it is fine. Â Here is what I currently have 0000-0099,1100-1199, 2200-2299 etc. Â I'm not sure if I can add any other numbers otherwise there will be more than 2 numbers in the same place in common. Â Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/236801-find-if-two-digits-in-same-place-are-the-same/#findComment-1217608 Share on other sites More sharing options...
requinix Posted May 19, 2011 Share Posted May 19, 2011 Are you comparing every number against every other number? Or do you want a set of numbers that don't share three or more digits with some other, specific number? Quote Link to comment https://forums.phpfreaks.com/topic/236801-find-if-two-digits-in-same-place-are-the-same/#findComment-1217704 Share on other sites More sharing options...
markjoe Posted May 19, 2011 Share Posted May 19, 2011 Yea I'm not sure I get it either but here's my guess at a (crude) stating point for you to check the numbers. You should be able to do it with modulus checks also. function numPlacesMatch($a, $b){ $matches = 0; $a = zerofill($a, 4); $b = zerofill($b, 4); for($i = 0; $i < 4; $i++){ if($a{$i} == $b{$i}){ $matches++; } } return $matches; } function zerofill($num, $places){ $str = (string)$num; while(strlen($str) < $places){ $str = '0' . $str; } return $str; } Quote Link to comment https://forums.phpfreaks.com/topic/236801-find-if-two-digits-in-same-place-are-the-same/#findComment-1217795 Share on other sites More sharing options...
markjoe Posted May 19, 2011 Share Posted May 19, 2011 my curiosity got the better of me, this one is way faster than messing with all the strings. function numPlacesMatch2($a, $b){ $matches = 0; if($a % 10 == $b % 10){ $matches++; } if(floor($a/10) % 10 == floor($b/10) % 10){ $matches++; } if(floor($a/100) % 10 == floor($b/100) % 10){ $matches++; } if(floor($a/1000) % 10 == floor($b/1000) % 10){ $matches++; } return $matches; } Quote Link to comment https://forums.phpfreaks.com/topic/236801-find-if-two-digits-in-same-place-are-the-same/#findComment-1217806 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.