In JS, not so good cause I'm new to JS. If any good writing comment please.
const sum = (accumulator, currentValue) => accumulator + currentValue; function range(size, startAt = 0) { return [...Array(size).keys()].map(i => i + startAt); } function sumDivisions(n){ arr = range(n-1, startAt=1).filter(x => n % x == 0) return arr.reduce(sum, 0); } function is_perfect_number(n){ return sumDivisions(n) == n } range(10000, 1).forEach(function(x){ if(is_perfect_number(x)){ console.log(x); } })
Do you have a better version for Go?
package main import ( "fmt" ) func sum_divisions(n int) int { sum := 0 for i := 1; i < n; i++ { if n % i == 0 { sum += i } } return sum } func is_perfect(n int) bool { return n == sum_divisions(n) } func main() { for i := 1; i <= 10000; i++ { if is_perfect(i) { fmt.Println(i) } } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In JS, not so good cause I'm new to JS. If any good writing comment please.
Do you have a better version for Go?