Arxci
Arxci

Reputation: 210

How to find the smallest four digit number, whose digits dont repeat, but add up to a randomized number

I need to take a number, lets say 6, and then find the smallest 4 digit number, whose digits do not repeat and add up to 6.

For example(These will not add up to the same number):

1023
3045
2345

These numbers are all ok because their digits do not repeat and are four digits

While:

1122
3344
123

These are not ok, because they either are not four digits or their numbers repeat

I'm currently at a roadblock where I can find said four digit number, but one: it is in a list which the program i need to plug this into wont accept and two: the digits aren't in the same order as the answers on the program (ie the smallest four digit number that does not have repeat digits, but adds up to six is 1023, but my program returns 0123, which is incorret.

Here is my current code:

x = 6
Sum = 0

#Find the four digit number
for i in range (999, 10000):
    #Find the sum of those numbers
    Sum = sum(map(int, str(i)))
    #Check if sum is = to x 
    if Sum == x:
        num = i
        #Get rid of any identical numbers
        result = list(set(map(int, str(num))))
        #Make sure the length is 4
        if len(result) == 4:
            print(result)
            #Output [0,1,2,3]     

Any help on how I could change this to work for what I want would be great

Upvotes: 1

Views: 1135

Answers (5)

Georgina Skibinski
Georgina Skibinski

Reputation: 13397

Using recursive algorithm:

def get_num(n, s, v=""):
    for el in range(1 if v=="" else 0, 10):
        if str(el) not in v:
            temp_sum=sum(int(l) for l in v+str(el))
            if(temp_sum>s):
                break
            elif len(v)==n-1:
                if(temp_sum==s):
                    return v+str(el)
            else:
                ret=get_num(n, s, v+str(el))
                if(ret): return ret

Outputs:

print(get_num(4,6))

>> 1023

Upvotes: 2

Rodolfo FR
Rodolfo FR

Reputation: 21

Since you need the smallest integer, you can stop the search at the first encountered with a break:

sum=6 # The sum of digits required.
found=0
for i in range(1000, 10000): # i from 1000 to 9999.
  a=i%10;
  b=i//10%10
  c=i//100%10
  d=i//1000%10
  if (a!=b)&(a!=c)&(a!=d)&(b!=c)&(b!=d)&(c!=d)&(a+b+c+d==sum):
    found=True
    break
if found:
  print(i)
else:
  print('Not found such a number.')

Upvotes: 1

mathfux
mathfux

Reputation: 5949

You might be interested in itertools package which is designed for solving combinatoric problems like yours.

for n in combinations(range(10),4):
    if sum(n)==6 and n[0]!=0:
        print(n)
        break

combinations method used here returns a generator of all tuples of length 4 that contains distinct digits from 0 to 9, they are sorted alphabetically.

You need to use from itertools import combinations to make it work.

Upvotes: 1

mzavarez
mzavarez

Reputation: 61

Changed your code a little:

x = 6
Sum = 0
result={}
#Find the four digit number
for i in range (999, 10000):
    #Find the sum of those numbers
    Sum = sum(map(int, str(i)))
    #Check if sum is = to x 
    if Sum == x:
        num = i
        aux = ''.join(list(set(map(str, str(num)))))
        if not aux in result:
          result[aux] = []
        result[aux].append(i)

for k in result:
  print(k, min(result[k]))

Upvotes: 2

Dan
Dan

Reputation: 1587

I've changed your program to print i instead of result, and break out of the loop when it finds the first number (which logically must be the smallest):

x = 6
Sum = 0

#Find the four digit number
for i in range (999, 10000):
    #Find the sum of those numbers
    Sum = sum(map(int, str(i)))
    #Check if sum is = to x
    if Sum == x:
        num = i
        #Get rid of any identical numbers
        result = list(set(map(int, str(num))))
        #Make sure the length is 4
        if len(result) == 4:
            print(i)
            break

This program will print 1023.

You could neaten it up a bit by turning it into a function with x as a parameter and returning the result instead of breaking and printing. Also it looks as if the num variable is redundant, you can just stick with i.

Upvotes: 2

Related Questions