Skip to content

Instantly share code, notes, and snippets.

@kutyel
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kutyel/8bf12c57acd005b2992c to your computer and use it in GitHub Desktop.
Save kutyel/8bf12c57acd005b2992c to your computer and use it in GitHub Desktop.
This is the second challenge of the Tuenti Contest 2015 solved in Node.js
'use strict';
var fs = require('fs');
var byline = require('byline');
var progress = require('progress');
var stream = byline(fs.createReadStream('challenge_2_input.txt', { encoding: 'utf8' }));
var total = 0;
var count = 0;
function semiPrime(n) {
var aux = 0;
for (var i = 2; i <= n; i++) {
while (n % i == 0) {
if (aux == 2) {
return false;
}
aux++;
n /= i;
}
}
return aux == 2;
}
stream.on('data', function (line) {
var output = 0;
if (count == 0) {
total = parseInt(line);
}
else {
var len = parseInt(line.split(' ')[1]);
var bar = new progress('calculating... ' + count + '/' + total + ' [:bar] :percent', {
complete: '/',
incomplete: ' ',
width: 50,
total: len
});
for (var i = parseInt(line.split(' ')[0]); i <= len; i++) {
if (semiPrime(i)) {
output++;
bar.tick(i * 100 / len); // show progress
}
}
fs.appendFileSync('challenge_2_output.txt', output + '\n');
}
count++;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment