Skip to content

Instantly share code, notes, and snippets.

@CarlLee
Last active December 16, 2015 13:38
Show Gist options
  • Save CarlLee/5442868 to your computer and use it in GitHub Desktop.
Save CarlLee/5442868 to your computer and use it in GitHub Desktop.
nimbusbase interview problems
//output:
//906609
//993*913
//cost: 0.122 secs
var start = new Date()
var last = 0;
var lastTxt = '';
for (var i = 999; i > 99; i--){
for(var j = 999; j > 99; j--){
var val = i*j;
if(test('' + val)){
if(val > last){
last = val
lastTxt = i + '*' + j;
}
}
}
}
console.log(last);
console.log(lastTxt);
console.log('cost: ' + (new Date() - start) / 1000 + ' secs')
function test(text){
var head = 0;
var tail = text.length - 1;
var r = true;
while( head < tail){
if(text[head] != text[tail]){
r = false;
break;
}
head++;
tail--;
}
return r;
}
#output:
#(837799, 524)
#cost: 3.41419506073 secs
import time
start = time.time()
calced = [-1] * 1000001
last_max_count = 0
last_max_int = 0
for i in xrange(1,1000001):
temp = i
count = 0
while True:
if temp % 2 == 0:
temp = temp / 2
else:
temp = temp * 3 + 1
count = count + 1
if temp == 1:
if last_max_count < count:
last_max_int = i
last_max_count = count
break
if temp < len(calced) and calced[temp] > 0:
count = count + calced[temp]
if last_max_count < count:
last_max_int = i
last_max_count = count
break
calced[i] = count
print (last_max_int, last_max_count)
print 'cost: %s secs' % (time.time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment