Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
18
Task/Babbage-problem/JavaScript/babbage-problem-1.js
Normal file
18
Task/Babbage-problem/JavaScript/babbage-problem-1.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Every line starting with a double slash will be ignored by the processing machine,
|
||||
// just like these two.
|
||||
//
|
||||
// Since the square root of 269,696 is approximately 519, we create a variable named "n"
|
||||
// and give it this value.
|
||||
n = 519
|
||||
|
||||
// The while-condition is in parentheses
|
||||
// * is for multiplication
|
||||
// % is for modulo operation
|
||||
// != is for "not equal"
|
||||
while ( ((n * n) % 1000000) != 269696 )
|
||||
n = n + 1
|
||||
|
||||
// n is incremented until the while-condition is met, so n should finally be the
|
||||
// smallest positive integer whose square ends in the digits 269,696. To see n, we
|
||||
// need to send it to the monitoring device (named console).
|
||||
console.log(n)
|
||||
111
Task/Babbage-problem/JavaScript/babbage-problem-2.js
Normal file
111
Task/Babbage-problem/JavaScript/babbage-problem-2.js
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// babbageNumbers :: Int -> [Int]
|
||||
const babbageNumbers = n =>
|
||||
// Take the first n Babbage numbers,
|
||||
take(n)(
|
||||
// from the concatenation of outputs of
|
||||
// a function which constructs the next number
|
||||
// ending in 269696, and returns it wrapped
|
||||
// in a list if it is a perfect square,
|
||||
// or just returns an empty list if it
|
||||
// is not a perfect square.
|
||||
// The concatenation of the map output eliminates
|
||||
// all empty lists, leaving a sequence of perfect
|
||||
// squares which end in 269696.
|
||||
concatMap(x => {
|
||||
const
|
||||
fx = 269696 + (1000000 * x),
|
||||
root = Math.sqrt(fx);
|
||||
return root === Math.floor(root) ? (
|
||||
[Tuple(root)(fx)]
|
||||
) : [];
|
||||
// Mapped over non-finite integer series
|
||||
// starting with 1.
|
||||
})(enumFrom(1))
|
||||
);
|
||||
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
const main = () =>
|
||||
// List of the first 10 positive integers
|
||||
// whose squares end in 269696.
|
||||
unlines(
|
||||
map(pair => fst(pair) + '^2 -> ' + snd(pair))(
|
||||
babbageNumbers(10)
|
||||
));
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// Tuple (,) :: a -> b -> (a, b)
|
||||
const Tuple = a => b => ({
|
||||
type: 'Tuple',
|
||||
'0': a,
|
||||
'1': b,
|
||||
length: 2
|
||||
});
|
||||
|
||||
// concatMap :: (a -> [b]) -> Gen [a] -> Gen [b]
|
||||
const concatMap = f =>
|
||||
// Instance of concatMap for non-finite streams.
|
||||
function*(xs) {
|
||||
let
|
||||
x = xs.next(),
|
||||
v = undefined;
|
||||
while (!x.done) {
|
||||
v = f(x.value);
|
||||
if (0 < v.length) {
|
||||
yield v[0];
|
||||
}
|
||||
x = xs.next();
|
||||
}
|
||||
};
|
||||
|
||||
// enumFrom :: Enum a => a -> [a]
|
||||
function* enumFrom(x) {
|
||||
let v = x;
|
||||
while (true) {
|
||||
yield v;
|
||||
v = succ(v);
|
||||
}
|
||||
}
|
||||
|
||||
// fst :: (a, b) -> a
|
||||
const fst = tpl => tpl[0];
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = f => xs =>
|
||||
(Array.isArray(xs) ? (
|
||||
xs
|
||||
) : xs.split('')).map(f);
|
||||
|
||||
// root :: Tree a -> a
|
||||
const root = tree => tree.root;
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
// succ :: Enum a => a -> a
|
||||
const succ = x =>
|
||||
1 + x;
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
// take :: Int -> String -> String
|
||||
const take = n => xs =>
|
||||
'GeneratorFunction' !== xs.constructor.constructor.name ? (
|
||||
xs.slice(0, n)
|
||||
) : [].concat.apply([], Array.from({
|
||||
length: n
|
||||
}, () => {
|
||||
const x = xs.next();
|
||||
return x.done ? [] : [x.value];
|
||||
}));
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue