Pergunta de entrevista da empresa Dynata

Programming Assignment: Given two numbers, M and N, write a program to determine if M & N are amicable pairs. An amicable pair (m,n) consists of two integers m,n for which the sum of proper divisors (the divisors excluding the number itself) of one number equals the other. Amicable pairs are occasionally called friendly pairs (Hoffman 1998, p. 45), although this nomenclature is to be discouraged since the numbers more commonly known as friendly pairs are defined by a different, albeit related, criterion. Not, a number cannot be amicable with itself.

Resposta da entrevista

Sigiloso

27 de set. de 2015

static boolean areAmicable(int first, int second) { if (first == second) return false; int firstSum = sumFactorsFrom(first, first-1) int secondSum = sumFactorsFrom(second, second-1) if (firstSum == second && secondSum == first) { return true } return false } static int sumFactorsFrom(number, index) { if (index == 0) { return 0 } int sum = sumFactorsFrom(number, index - 1) if (number % index == 0) { sum += index } return sum }