Created
December 10, 2013 21:15
-
-
Save H2CO3/7900344 to your computer and use it in GitHub Desktop.
Project Euler problem #42 solution in Sparkling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* p42.spn | |
* Solution to Project Euler problem #42 | |
* | |
* Created by Arpad Goretity on 10/12/2013 | |
*/ | |
function FileSize(file) | |
{ | |
fseek(file, 0, "end"); | |
var n = ftell(file); | |
fseek(file, 0, "set"); | |
return n; | |
} | |
function GetWordsFromFile(fname) | |
{ | |
var f = fopen(fname, "rb"); | |
var words = array(); | |
var cont = fread(f, FileSize(f)); | |
var qwords = split(cont, ","); | |
enumerate(qwords, function(k, v, tmp) { | |
tmp[k] = substr(v, 1, sizeof v - 2); | |
}, words); | |
fclose(f); | |
return words; | |
} | |
function StringValue(str) | |
{ | |
var i; | |
var n = sizeof str; | |
var v = 0; | |
for i = 0; i < n; i++ { | |
v += str[i] - 'A' + 1; | |
} | |
return v; | |
} | |
function IsTriangleNumber(n) | |
{ | |
var i; | |
for i = 0; i * (i + 1) / 2 <= n; i++ { | |
if i * (i + 1) / 2 == n { | |
return true; | |
} | |
} | |
return false; | |
} | |
var n = 0; | |
var ws = GetWordsFromFile("words.txt"); | |
var nws = sizeof ws; | |
var i; | |
for i = 0; i < nws; i++ { | |
if IsTriangleNumber(StringValue(ws[i])) { | |
n++; | |
} | |
} | |
print(n); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment