Skip to content

Instantly share code, notes, and snippets.

@mfelsche
Created October 7, 2024 06:39
Show Gist options
  • Save mfelsche/7cde41387d74f25eff36ff294875492f to your computer and use it in GitHub Desktop.
Save mfelsche/7cde41387d74f25eff36ff294875492f to your computer and use it in GitHub Desktop.
actor Main
var count : U64 = 0
new create(env: Env) =>
var n: F64 = 40
var start: F64 = 1
var k: F64 = 24
try n = env.args(1)?.f64()? end
try k = env.args(2)?.f64()? end
env.out.print("N: "+n.string()+" K: "+k.string())
env.out.print("Below are the following consecutive Ranges,\nwhere the sum of squares is perfect square\n")
try while start<=n do
var p = Worker
p.calculate(env,start,k,this)
start = start + 1
end then
env.out.print("Count Val:"+this.count.string())
end
be countIncrement(env: Env) =>
count = count +1
env.out.print(count.string())
actor Worker
var count: U64 = 0
be calculate(env: Env,q: F64, k: F64,pm: Main) =>
var p: F64 = q
if q == 1 then
var r = ((k+(q-1))*(k+q)*((2*(k+(q-1)))+1))/6
getResult(env,r,k,q,pm)
else
var r: F64 = 0
var temp1: F64 = 0
var temp2: F64 = 0
temp2 = ((k+(q-1))*(k+q)*((2*(k+(q-1)))+1))/6
temp1 = ((q-1)*(q)*((2*(q-1))+1))/6
getResult(env,temp2-temp1,k,q,pm)
end
be getResult(env: Env, n: F64, k: F64, q: F64,pm: Main) =>
var sqrtFloatingVal: F64 = n.sqrt() // taking square root of p
var integerVal: I64 = sqrtFloatingVal.i64()
var squareIntVal: I64 = integerVal*integerVal
var squareFloatingVal: F64 = squareIntVal.f64()
if n == squareFloatingVal then
pm.countIncrement(env)
env.out.print("Range is "+q.string()+" to "+(q+(k-1)).string()+" = result Square val:"+n.string()+" sqrt:"+(n.sqrt()).string())
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment