
Originally Posted by
djr33
That's not too bad doing a loop where you generate each number then throw out the ones that are bad. It would be much harder (and probably mathematically interesting) doing this by looping through only the good numbers.
I did something else recently that was similar. I don't remember exactly what the task was, but it was so much easier looping through all numbers then throwing them out when they failed a test rather than generating them 'correctly' the first time around.
Then the trick can be making this recursive. That's where a second loop to throw out the bad ones becomes the real time saver for the programmer, although perhaps not the 'best' way to do it.
I would've tried this today, but I was busy on another project. And now John has done it so that should be enough.
I think it might have been in a thread calculating odds, perhaps in the PHP section, ring any bells? I don't think I contributed to that thread, but was lurking in it.
However, this challenge stipulates that we list all the good numbers and all the bad numbers. Unless I misunderstood that, we can't skip anything.
But, in the challenge author's blog there is a response page:
http://beust.com/weblog/2008/08/28/c...lenge-wrap-up/
where several or more elegant and not so elegant solutions are proffered in various languages. It looks to me, even though I'm no expert in those languages, that at least some of the solutions, even the ones he lists first as having impressed him in one way or another, seem to play fast and loose with the original stipulations. I see a number that don't look like they output anything.
Javascript and PHP will never approach the speeds of some of those other languages. C/C++ are probably about the industry standard for speed. That one called j I never heard of, neither had Cedric, but he checked and it's a real language. It might be faster.
And on that blog page, speed had become the main criteria, as he upped the ante to 10 to the tenth as the minimum number to count to. Just using 1000000, will crash Firefox with my javascript method. It gets the right answer, but the browser is useless after that. And it takes forever.
I've tightened the code up a little and noticed that it runs faster on Chrome, 1000000 runs OK there with the new code, but there's a noticeable lag of a few moments and refreshing the page causes a crash imminent warning, I selected to skip the page and went back to 10000, then it was OK again in Chrome:
Code:
<!DOCTYPE html>
<html>
<head>
<title>Beustiality</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
table {
border: 2px solid #ccc;
}
td {
border: 1px solid #555;
}
#good, #bad {
vertical-align: top;
}
th {
font: bold 95% sans-serif;
}
</style>
<script type="text/javascript">
// Cedric Beust's Challenge Script (c)2012 John Davenport Scheuer
// as first seen in http://www.dynamicdrive.com/forums/
// username: jscheuer1 - This Notice Must Remain for Legal Use
function beust(){
var start = 1, max = 10000, good = [], bad = [], gap = [0, 0], count, gapln, str, temp, i, ok;
count = start - 1;
while(count < max){
ok = true;
str = (++count).toString(10);
temp = str.split('');
i = temp.length;
while(temp[--i]){
if(str.indexOf(temp[i]) !== i){
bad.push(str);
ok = false;
if(gap[1] < (gapln = count - good[good.length - 1])){
gap = [count, gapln];
}
break;
}
}
if(ok){good.push(str);}
}
return {
start : start,
max : max,
gnum : good.length,
good : good.join(', '),
bnum : bad.length,
bad : bad.join(', '),
biggest : gap[1] + ', from: ' + (gap[0] - gap[1] + 1) + ' to: ' + gap[0]
};
}
</script>
</head>
<body>
<table>
<tr>
<th colspan=2 id="title"></th>
</tr>
<tr>
<th>Good</th><th>Bad</th>
</tr>
<tr>
<td id="totg"></td><td id="totb"></td>
</tr>
<tr>
<td id="good"></td><td id="bad"></td>
</tr>
</table>
<script type="text/javascript">
(function(){
var data = beust();
document.getElementById('title').innerHTML = 'Cedric\'s Code Challenge from: ' + data.start + ' to: ' + data.max;
document.getElementById('totg').innerHTML = 'Total Good: ' + data.gnum;
document.getElementById('totb').innerHTML = 'Total Bad: ' + data.bnum + '<br>Largest gap was: ' + data.biggest + ', inclusive';
document.getElementById('good').innerHTML = data.good;
document.getElementById('bad').innerHTML = data.bad;
})();
</script>
</body>
</html>
http://home.comcast.net/~jscheuer1/side/beust.htm
Bookmarks