Harshad Numbers

A Harshad Number in a given number base is an integer that is divisible by the sum of its digits when written in that base.

For example, the number 11 in base 5 (which is 6 in base 10) is divisible by 1 + 1 = 2 in base 5. It is therefore a Harshad number in base 5.

1 is the first Harshad number in base 5. Let H be the 1000th Harshad Number in base 5. What are the first 3 digits of H?

Details and assumptions

Remember: H is in base 5!


The answer is 114.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

The 1000th Harshad number is 114040 (this is in base 5 representation). The first three digits are 114 .

One solution is to iterate through the base 10 numbers, and check if they are base 5 Harshad numbers. Continue until you find the 1000th base 5 Harshad. Make sure to give your answer in base 5 representation.

To check if a base 10 number N is a Harshad in base 5, first convert N to base 5. Let F be the base 5 representation of N . Second, add up the digits of F and represent the sum S in base 10. Finally, if N modulo S is 0, then they are divisible and F is a Harshad number in base 5.

# Python solution

# to_base_5()
# This is a function to convert base 10 numbers to a base 5 string representation
#
# to_base_5(5) returns "10"
# to_base_5(26) returns "101"
import math
def to_base_5(n):
    s=[]

    # this builds the base 5 string backwards
    while n:
        # find the smallest digit of n_5 and append to the string
        s.append(str(n % 5))
        n = math.floor(n / 5)

    # reverse the string before returning
    return ''.join(s[::-1])

# isb5Harshad(n)
# returns True if n is a base5Harshad, False otherwise
def isb5Harshad(n):
    # convert to base 5 string representation
    nb5string = to_base_5(n)

    # add the digits to get the 'divisor'
    divisor = 0 # divisor is in base 10
    for d in nb5string:
           # convert d from base 5 string to a base 10 integer, then add it to divisor
           divisor += int(d, 5) 

    # check if 'n' is evenly divisible by 'divisor'
    if n % divisor == 0:
        return True
    else:
        return False        

# nth_base5_Harshad(n)
# returns a string representing the nth Harshad number in base 5. 
def nth_base5_Harshad(n):
    numb5Harshads = 0
    c = 0

    # when this loop exits, c will contain the nth base 5 Harshad
    while (numb5Harshads < n):
        c += 1
        if isb5Harshad(c):
            numb5Harshads += 1

    return to_base_5(c)

print nth_base5_Harshad(1000)
Alex Li
Feb 13, 2015

Java code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
public class x
{

    public static void main(String[] args)
    {
        int z = 0;
        int i = 0;
        for(int a0 = 0; a0 <= 4; a0++)
        {
            for(int a = 0; a<=4; a++)
            {
                for(int b = 0; b<=4; b++)
                {
                    for(int c = 0; c<=4; c++)
                    {
                        for(int d = 0; d<=4; d++)
                        {
                            for(int e = 0; e<=4; e++)
                            {
                                z = 3125*a0+625*a+125*b+25*c+5*d+e;
                                if((a0+a+b+c+d+e != 0) && z % (a0+a+b+c+d+e) == 0)
                                {
                                    i++;
                                    if(i<=1200)
                                    {
                                        System.out.println(i + " " + z);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Now, when the program runs, the line 1000 4270 is printed. Converting 4270 4270 to base 5 5 , we get 4270 = 1142 0 5 = > 114 4270=11420_5=>\boxed{114}

Chew-Seong Cheong
Aug 15, 2014

The 100 0 t h 1000^{th} Harshad number in base 5 5 is 114 040 \boxed{114}040 . I used the following Python coding. Please note that the digital sum in base 5 5 is same as that in base 10 10 .

x = 0

n = -1

for a in range (2):

for b in range (5):
    for c in range (5):
        for d in range(5):
            for e in range(5):
                for f in range(5):
                    n += 1
                    h = str(a)+str(b)+str(c)+str(d)+str(e)+str(f)
                    s = 0
                    for i in range(len(h)):
                        s += int(h[i])
                    if n > 0 and n % s == 0:
                        x += 1
                        print x, h, n, s
Daniel Lim
Jun 29, 2014

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
using namespace std;

int base5to10(int n){
    stringstream ss;
    ss << n;
    string str = ss.str();

    int base10 = 0;
    for (int i = 0; i < str.size(); i++){
        int power = str.size() - 1 - i;
        int val = str[i] - '0';
        base10 += pow(5, power)*val;
    }

    return base10;
}

int base10to5(int x) {
    string str;
    bool converted = false;
    while (!converted){
        if (x / 5 > 0){
            int dg = x % 5;
            char digit = dg + '0';
            str += digit;
            x /= 5;
        }
        else{
            int dg = x % 5;
            char digit = dg + '0';
            str += digit;
            converted = true;
        }
    }
    reverse(str.begin(), str.end());
    int base5 = stoi(str);
    return base5;
}

int digitSum(int x){
    stringstream ss;
    ss << x;
    string str = ss.str();

    int sum = 0;
    for (int i = 0; i < str.size(); i++){
        int val = str[i] - '0';
        sum += val;
    }

    return sum;
}

bool isHarshad(int dec){
    int base5 = base10to5(dec);
    int sum = digitSum(base5);

    if (dec%sum==0)
        return true;
    return false;
}

int nthHarshad(int n){
    int counter = 0;
    int dec = 0;
    while (counter < n){
        dec++;
        if (isHarshad(dec))
            counter++;
    }

    return base10to5(dec);
}

int main(){

    freopen("answer.in", "r", stdin);
    freopen("answer.out", "w", stdout);

    int n; cin >> n;

    cout << nthHarshad(n);

    return 0;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...