code.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* A centrifuge, as you probably know, is a laboratory device used to
  2. * separate fluids based on density. The separation is achieved through
  3. * centripetal force by spinning a collection of test tubes at high speeds.
  4. * This means, the configuration needs to be in balance.
  5. * Create a function that takes two numbers as arguments n and k and
  6. * returns true if the configuration is balanced and false if it's not.
  7. * To check out the formula, look at the resources tab.
  8. * Here are the valid configurations for n = 6, k = 2, 3, 4 and 6.
  9. * One test tube (k = 1) is never in balance.
  10. * One hole (n = 1) is never in balance, even empty.
  11. */
  12. std::vector<int> factors(int n){
  13. int z = 2;
  14. std::vector<int> output;
  15. while (z * z <= n){
  16. if (n % z == 0){
  17. output.push_back(z);
  18. n /= z;
  19. }else{z++;}
  20. }
  21. if(n > 1){output.push_back(n);}
  22. return output;
  23. }
  24. // It(test3){Assert::That(cFuge(12, 7), Equals(true));}
  25. bool cFuge(int n, int k) {
  26. int Sum = 0;
  27. // if k or n is one, return false
  28. if(1 == k || 1 == n){return false;}
  29. if(0 == k){return true;}
  30. if(n == k){return true;}
  31. // if k and n are even, return true
  32. if ((k%2 == 0) && (n%2 == 0)){return true;}
  33. std::vector<int> nfactors = factors(n);
  34. // find the prime factorization of a number and see if the sum of those numbers is equal to k
  35. for(int i = 0;i<nfactors.size();i++){Sum += nfactors[i];}
  36. if(Sum == k){return true;}
  37. std::vector<int> kfactors = factors(k);
  38. // if n & k share a common factor, return true.
  39. for(int i = 0;i<nfactors.size();i++){
  40. for(int j = 0;j<kfactors.size();j++){
  41. if(nfactors[i] == kfactors[j]){return true;}
  42. }
  43. }
  44. return false;
  45. }