Dima Kabaruhin Dima Kabaruhin - 1 month ago 8
Python Question

How it works in python? ruby code

I don't understand what it code doing, please help.

How it will be working in python or in another simple language?

a = []
a << [1]

for i in 2..10001
f = 0
a.each{ |group|
m = 1
group.each { |c|
m *= i % c
}
f += m
if m > 0
group << i
break
end
}
a << [i] if f == 0
end

p a
p a.size

Answer

Literally translated to python this is:

a = []
a.append([1])

for i in range(2,10001 + 1):
    f = 0
    for group in a:
        m = 1
        for c in group:
            m *= i % c
        f += m
        if m > 0:
            group.append(i)
            break
    if f == 0:
        a.append([i])

print a
print len(a)