Skip to content

Instantly share code, notes, and snippets.

@Steph-crown
Created October 16, 2020 20:27
Embed
What would you like to do?
Calculates product of prime factors of a number
///Checks if a number is prime
bool checkPrime(x) {
int factors = 0;
int i = 1;
//He number of factors of the number
while (i <= x) {
factors = x % i == 0 ? factors + 1 : factors;
i++;
}
//If there are only 2 factors, it is prime
return factors == 2;
}
int primeProduct(num) {
int i = 2;
if (num < 2) {return 0;}
int product = 1;
while (i <= num) {
product = checkPrime(i) ? product * i : product;
i++;
}
return product;
}
void main() {
print(primeProduct(0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment