Created
October 16, 2020 20:27
-
-
Save steph-crown/03b2276804a47988780042d48171938f to your computer and use it in GitHub Desktop.
Calculates product of prime factors of a number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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