I was posed this question by a friend;friend:
I am a natural number written with 10 different digits
such that for all i ∈ {1, . . ., 10} the number formed
by my i first digits (from left to right) is divisible
by i. Who am I?
I am a natural number written with 10 different digits such that for all \$i ∈ {1, . . ., 10}\$ the number formed by my \$i\$ first digits (from left to right) is divisible by \$i\$. Who am I?
I came up with a brute-force solution over the next hour or so (a few attempts failed) and came eventually finished with this;this:
# !/usr/bin/python
# -*- coding: utf-8 -*-
# build all possible permutations
import itertools
base_number = ['1','2','3','4','5','6','7','8','9','0']
perms = itertools.permutations(base_number)
# join the permutation lists into single numbers by joining each into a string
all_nums = [''.join(item[0:10]) for item in perms]
'''
Check whether each item is divisible by i (from 10-->2) for subset of item,
item[0:i]. For example;
i = 10, number = 1234567890
number[0:10] = 1234567890
is this divisible by i(10)? Yes.
i = 9, number = 1234567890
number[0:9] = 123456789
is this divisible by i(9)? Yes.
i = 8, number = 1234567890
number[0:8] = 12345678
is this divisible by i(8)? No.
We do not check whether the number is divisible by 1 as all integers are
divisible by 1.
'''
for i in range(10,1,-1):
legits = [x for x in all_nums if int(x[0:i])%i==0]
all_nums = legits
print "%-10d # of result can be divided by %d" % (len(legits),i)
print legits
f(1) = {1, 2, 3, 4, 5, 6, 7, 8, 9}
f(2) = for n in f(1):
find solutions to i for $$n * 10 + i = 0 (mod 2)$$
where i isn't a digit of n.
WelcomingI welcome all feedback, but I'm specifically hoping to hear about my coding-style/practices and the algorithm I developed.
I was posed this question by a friend;
I am a natural number written with 10 different digits
such that for all i ∈ {1, . . ., 10} the number formed
by my i first digits (from left to right) is divisible
by i. Who am I?
I came up with a brute-force solution over the next hour or so (a few attempts failed) and came eventually finished with this;
# !/usr/bin/python
# -*- coding: utf-8 -*-
# build all possible permutations
import itertools
base_number = ['1','2','3','4','5','6','7','8','9','0']
perms = itertools.permutations(base_number)
# join the permutation lists into single numbers by joining each into a string
all_nums = [''.join(item[0:10]) for item in perms]
'''
Check whether each item is divisible by i (from 10-->2) for subset of item,
item[0:i]. For example;
i = 10, number = 1234567890
number[0:10] = 1234567890
is this divisible by i(10)? Yes.
i = 9, number = 1234567890
number[0:9] = 123456789
is this divisible by i(9)? Yes.
i = 8, number = 1234567890
number[0:8] = 12345678
is this divisible by i(8)? No.
We do not check whether the number is divisible by 1 as all integers are
divisible by 1.
'''
for i in range(10,1,-1):
legits = [x for x in all_nums if int(x[0:i])%i==0]
all_nums = legits
print "%-10d # of result can be divided by %d" % (len(legits),i)
print legits
f(1) = {1, 2, 3, 4, 5, 6, 7, 8, 9}
f(2) = for n in f(1):
find solutions to i for $$n * 10 + i = 0 (mod 2)$$
where i isn't a digit of n.
Welcoming all feedback, but I'm specifically hoping to hear about my coding-style/practices and the algorithm I developed.
I was posed this question by a friend:
I am a natural number written with 10 different digits such that for all \$i ∈ {1, . . ., 10}\$ the number formed by my \$i\$ first digits (from left to right) is divisible by \$i\$. Who am I?
I came up with a brute-force solution over the next hour or so (a few attempts failed) and came eventually finished with this:
# !/usr/bin/python
# -*- coding: utf-8 -*-
# build all possible permutations
import itertools
base_number = ['1','2','3','4','5','6','7','8','9','0']
perms = itertools.permutations(base_number)
# join the permutation lists into single numbers by joining each into a string
all_nums = [''.join(item[0:10]) for item in perms]
'''
Check whether each item is divisible by i (from 10-->2) for subset of item,
item[0:i]. For example;
i = 10, number = 1234567890
number[0:10] = 1234567890
is this divisible by i(10)? Yes.
i = 9, number = 1234567890
number[0:9] = 123456789
is this divisible by i(9)? Yes.
i = 8, number = 1234567890
number[0:8] = 12345678
is this divisible by i(8)? No.
We do not check whether the number is divisible by 1 as all integers are
divisible by 1.
'''
for i in range(10,1,-1):
legits = [x for x in all_nums if int(x[0:i])%i==0]
all_nums = legits
print "%-10d # of result can be divided by %d" % (len(legits),i)
print legits
f(1) = {1, 2, 3, 4, 5, 6, 7, 8, 9}
f(2) = for n in f(1):
find solutions to i for $$n * 10 + i = 0 (mod 2)$$
where i isn't a digit of n.
I welcome all feedback, but I'm specifically hoping to hear about my coding-style/practices and the algorithm I developed.
Finding 10-unique-digit number with i-first-digits divisible by i
I was posed this question by a friend;
I am a natural number written with 10 different digits
such that for all i ∈ {1, . . ., 10} the number formed
by my i first digits (from left to right) is divisible
by i. Who am I?
I came up with a brute-force solution over the next hour or so (a few attempts failed) and came eventually finished with this;
# !/usr/bin/python
# -*- coding: utf-8 -*-
# build all possible permutations
import itertools
base_number = ['1','2','3','4','5','6','7','8','9','0']
perms = itertools.permutations(base_number)
# join the permutation lists into single numbers by joining each into a string
all_nums = [''.join(item[0:10]) for item in perms]
'''
Check whether each item is divisible by i (from 10-->2) for subset of item,
item[0:i]. For example;
i = 10, number = 1234567890
number[0:10] = 1234567890
is this divisible by i(10)? Yes.
i = 9, number = 1234567890
number[0:9] = 123456789
is this divisible by i(9)? Yes.
i = 8, number = 1234567890
number[0:8] = 12345678
is this divisible by i(8)? No.
We do not check whether the number is divisible by 1 as all integers are
divisible by 1.
'''
for i in range(10,1,-1):
legits = [x for x in all_nums if int(x[0:i])%i==0]
all_nums = legits
print "%-10d # of result can be divided by %d" % (len(legits),i)
print legits
I considered that a recursive algorithm could perform this task much more efficiently but have not considered how to actually code the pseudo-code'ish solution I was shown;
f(1) = {1, 2, 3, 4, 5, 6, 7, 8, 9}
f(2) = for n in f(1):
find solutions to i for $$n * 10 + i = 0 (mod 2)$$
where i isn't a digit of n.
Welcoming all feedback, but I'm specifically hoping to hear about my coding-style/practices and the algorithm I developed.