Skip to content

Instantly share code, notes, and snippets.

@steph-crown
Created October 16, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steph-crown/03b2276804a47988780042d48171938f to your computer and use it in GitHub Desktop.
Save steph-crown/03b2276804a47988780042d48171938f to your computer and use it in GitHub Desktop.
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