Sort by

recency

|

172 Discussions

|

  • + 0 comments

    My Python solution:

    if __name__ == '__main__':
        n = int(input().strip())
        
        nums = [i for i in range(1, n+1) if n%i==0]
        best = 0
        max_sum = 0
        for x in nums:
            soma = sum(map(int,str(x)))
            if soma > max_sum:
                best, max_sum = x, soma
            elif soma == max_sum:
                best = min(x, best)
        
        print(best)
    
  • + 0 comments

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    char* readline(); char* ltrim(char*); char* rtrim(char*);

    int parse_int(char*);

    int main() { int n = parse_int(ltrim(rtrim(readline())));

    return 0;
    

    }

    char* readline() { size_t alloc_length = 1024; size_t data_length = 0;

    char* data = malloc(alloc_length);
    
    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);
    
        if (!line) {
            break;
        }
    
        data_length += strlen(cursor);
    
        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
            break;
        }
    
        alloc_length <<= 1;
    
        data = realloc(data, alloc_length);
    
        if (!data) {
            data = '\0';
    
            break;
        }
    }
    
    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    
        data = realloc(data, data_length);
    
        if (!data) {
            data = '\0';
        }
    } else {
        data = realloc(data, data_length + 1);
    
        if (!data) {
            data = '\0';
        } else {
            data[data_length] = '\0';
        }
    }
    
    return data;
    

    }

    char* ltrim(char* str) { if (!str) { return '\0'; }

    if (!*str) {
        return str;
    }
    
    while (*str != '\0' && isspace(*str)) {
        str++;
    }
    
    return str;
    

    }

    char* rtrim(char* str) { if (!str) { return '\0'; }

    if (!*str) {
        return str;
    }
    
    char* end = str + strlen(str) - 1;
    
    while (end >= str && isspace(*end)) {
        end--;
    }
    
    *(end + 1) = '\0';
    
    return str;
    

    }

    int parse_int(char* str) { char* endptr; int value = strtol(str, &endptr, 10);

    if (endptr == str || *endptr != '\0') {
        exit(EXIT_FAILURE);
    }
    
    return value;
    

    }

  • + 0 comments

    Kristen loves playing with and comparing numbers. She thinks that if she takes two different positive numbers, the one whose digits sum to a larger number is better than the other. If the sum of digits is equal for both numbers, then she thinks the smaller number is better. For example, Kristen thinks that is better than and that is better than .

    Given an integer, , can you find the divisor of that Kristin will consider to be the best?

    Input Format

    A single integer denoting .

    Constraints

    Output Format

    Print an integer denoting the best divisor of .

    Sample Input 0

  • + 0 comments

    Kristen's fascination with numbers is fascinating! Her method of comparing them based on the sum of their digits adds an intriguing twist. 11xplay

  • + 2 comments

    **Python 3: **

    def best_divisor(n):
        sums = []
        divisors = []
        for i in range(1, n + 1):
            if n % i == 0:
                divisors.append(i)
                sums.append(sum_digits(i))
    
        # index = sums.index(max(sums))
        # print(divisors[index])
        return divisors[sums.index(max(sums))]
    
    def sum_digits(n):
        sum = 0
        for d in str(n):
            sum += int(d)
    
        return sum