Least Squares
A Roc solution for the following Least Squares problem:
Write a program that prints the least positive integer number
n
, where the difference ofn*n
and(n-1)*(n-1)
is greater than 1000.
Code
app "example" packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.0/bkGby8jb0tmZYsy2hg1E_B2QrCgcSTxdUlHtETwm5m4.tar.br" } imports [ pf.Stdout, ] provides [main] to pf main = nStr = Num.toStr (leastSquareDifference {}) Stdout.line "The least positive integer n, where the difference of n*n and (n-1)*(n-1) is greater than 1000, is \(nStr)" ## A recursive function that takes an `U32` as its input and returns the least ## positive integer number `n`, where the difference of `n*n` and `(n-1)*(n-1)` ## is greater than 1000. ## ## The input `n` should be a positive integer, and the function will return an ## `U32`representing the least positive integer that satisfies the condition. ## leastSquareDifference : {} -> U32 leastSquareDifference = \_ -> findNumber = \n -> difference = (Num.powInt n 2) - (Num.powInt (n - 1) 2) if difference > 1000 then n else findNumber (n + 1) findNumber 1 expect leastSquareDifference {} == 501
Output
Run this from the directory that has main.roc
in it:
$ roc run The least positive integer n, where the difference of n*n and (n-1)*(n-1) is greater than 1000, is 501
Run unit tests with roc test main.roc