Eight problems to split into functions. Before you type, write down which
helpers you need and what each one takes and returns — then main almost writes
itself.
- Prime Predicate: up to 2·10⁴ numbers of up to 10⁹, some negative. The
d * d <= nloop is fast enough; trying every d up to n is not. - GCD Prime Check: two helpers, and one call inside the other —
isPrime(gcd(a, b)). Remember that 1 is not prime. - Primes with Digit Sum Divisible by 3: when no number qualifies, print only
the
0line. - Perfect, Abundant, or Deficient: the proper divisors are all the divisors except n itself. Add them up with divisor pairs, and take care when d and n / d are the same number. The number 1 has no proper divisors.
- Most Frequent Character: count the letters in an array of 26. On a tie the
smallest letter wins, so go from
atozand replace the best only when a count is strictly bigger. - Palindrome 2 asks for two things at once: one of the two strings is a
palindrome, and one of the two joined strings, s + t or t + s, is a palindrome
too. One
isPalindrome, four calls. - Operation Dispatcher: one function that gets the command as a
stringand the two numbers, and returns the result. The products fit in along long. - Rotate Matrix: the grid can be 500 × 500, a quarter of a million numbers — keep the fast-IO lines. Work out where the element of row i, column j of the answer comes from on a 2 × 2 and a 3 × 3 example first. When n = 0, print nothing.