Skip to content

Instantly share code, notes, and snippets.

@xk
xk / happyNumbers.c
Created February 10, 2014 23:43
Happy Numbers in C
//2014-02-10 [email protected] happy numbers in C
#include<stdio.h>
unsigned sumaCuadrados (unsigned n) {
unsigned d, r= 0;
while (n) d= n % 10, r+= d*d, n/= 10;
return r;
}
@xk
xk / happyNumbers.js
Created February 10, 2014 23:40
Concise Happy Numbers JavaScript
//2014-02-10 [email protected] concise happy numbers JavaScript
function happy (n) {
var past= [];
while (n= [].reduce.call(n.toString(), function (a,n) { return a+n*n }, 0))
if (n === 1) return 1; else if (past.indexOf(n) >= 0) return 0; else past.push(n);
}
for (var i=0 ; i<=100 ; ++i) happy(i) && console.log(i);
@xk
xk / fasterHappyNumbers.js
Created February 10, 2014 23:35
Faster Happy Numbers JavaScript
//2014-02-10 [email protected] Happy Numbers JavaScript
function sumaCuadrados (n) {
var d, r= 0;
while (n) n-= (d= n % 10), r+= d*d, n/= 10;
return r;
}
function isHappy (n) {
while (n && n!=1 && n!=4 && n!=16 && n!=37 && n!=58 && n!=89 && n!=145 && n!=42 && n!=20)
@xk
xk / happyNumbers.js
Created February 10, 2014 16:03
Happy Numbers in JavaScript
//2014-02-10 [email protected] happy numbers
String.prototype.reduce= [].reduce;
Array.prototype.has= function has (n) { return this.indexOf(n) >= 0 };
Array.prototype.times= function (f,i) { for (i= this[0] ; i<=this[1] ; i++) f(i) };
function happy (n) {
var past= [];
while (1) {
if ((n= n.toString().reduce(function(n,v){ return n+v*v },0)) === 1) return 1;
@xk
xk / CrossFunctionVarManipulation.c
Created February 9, 2014 17:50
Cross-function var manipulation
//2014-02-09 [email protected]
//Cross-function var manipulation
#include <stdio.h>
#include <string.h>
void uno () {
char* b= (char*) &b;
while (strncmp("Ehh", b, 3)) b++;
@xk
xk / StackFrameGarbage.c
Created February 9, 2014 17:47
stack frame garbage
//2014-02-09 [email protected]
//stack frame garbage
#include <stdio.h>
void uno () {
char a[18]= "GeorgeOfTheJungle";
}
void dos () {
@xk
xk / ManipulateStackFrameReturnAddress.c
Last active August 29, 2015 13:56
Manipulate the stack frame return address
//2014-02-09 [email protected]
//Manipulate the stack frame return address
#include <stdio.h>
#include <string.h>
void uno () {
char** r= (char**) &r+2;
while (**r != '\x90') (*r)++;
@xk
xk / gist:8073619
Created December 21, 2013 19:16
Pone la hora de finalización al lado del artículo
javascript:[].filter.call(document.getElementsByClassName("g-clTRght"), function (e) {
return /^[0-9]+[hHmMsM]/.test(e.innerHTML);
}).forEach(function (e) {
var r, h, p, hora, minuto, segundo;
var txt= e.innerHTML;
if (/^[0-9]+[hH]/.test(txt)) {
r= /^([0-9]+)[hH] ([0-9]+)[mM]/.exec(txt);
h= new Date(Date.now() + (r[1]*60*60*1000) + (r[2]*60*1000));
}
else if (/^[0-9]+[mM]/.test(txt)) {
@xk
xk / timers.js
Created September 28, 2013 12:13
Shows that node's timers are borken and execute out of order.
(function run () {
//2013.09.27 [email protected]
//Shows that node's timers are borken and often execute out of order
console.log('\nBEGIN');
var MAX= 666;
var last= 0;
var items= 0;
var errores= 0;
@xk
xk / plistread.js
Last active December 10, 2015 09:19 — forked from juanfal/plistread.rb
//plistread.js, 2012-12-30 [email protected]
var plist= require('plist');
var shell= require('child_process').exec;
var puts= console.log;
//process.argv[0] -> "node"
//process.argv[1] -> "plistread.js"
//process.argv[2] -> inputFile
//process.argv[3..n] -> property list keys