F-Flat: 20 Happy Numbers

node v8.17.0
version: 2.0.1
endpointsharetweet
const { Stack } = require('@hypercubed/f-flat@0.0.6'); const f = new Stack(); f.eval(` core: rcl use types: rcl use math: rcl use xxs: [ [ first ] [ rest ] bi ] ;; // This is an alternative to pattern matching // This, and pattern matching, will be coming soon /** * Calls the second quotation in the first pair whose first quotation yields a true value. */ cond: [ dup length 0 > [ xxs slip slipd [ cond ] >> branch ] when ] ; `).stack;
From Wikipedia: A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits in base-ten, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).
f.eval(` πŸ’ͺ: [ string // convert to a string '' / // split into characters [ number 2 ^ ] map // convert each to a number and square sum // sum the values ] ; πŸ€”: [ floor // ensure value is an integer [ [[dup 1 =] [drop true]] // true if === 1 [[dup 4 =] [drop false]] // false if === 4 [[true] [πŸ’ͺ πŸ€”]] // rinse and repeat otherwise ] cond ] ; πŸ˜€: [ integers // create a list of n integers [ πŸ€” ] filter // filter for πŸ˜€ integers ] ; `).stack.toString();
f.eval(` 100 πŸ˜€ `).stack.toString();
Loading…

no comments

    sign in to comment