How to find the factors of a number in Dart

1 Answer

0 votes
// Numbers that divide the original number exactly

Set<int> GetFactors(N) {
    Set<int> result = {};
 
    for (var i = 1; i <= N / i; i++) {
        if (N % i == 0) {
            result.add(i);
            result.add((N / i).floor());
        }
    }
 
    return result;
}
 
void main() {
    var N = 24;
  
    var result = GetFactors(N);
  
    print(result);
}
 
 
 
 
/*
run:
 
{1, 24, 2, 12, 3, 8, 4, 6}
 
*/

 



answered Oct 17, 2022 by avibootz

Related questions

...