某企业招聘题目:获取最小的10000个素因子只有2、3、5的数

jthmath · 2018-03-10 00:46:09 · 3872 次点击

N值用开三次方+1

for k := 0; k < N; k++ {
    for i := 0; i < N; i++ {
        for j := 0; j < N; j++ {
            f := math.Pow(2, float64(k)) * math.Pow(3, float64(i)) * math.Pow(5, float64(j))
            var r Info
            r[0] = float64(k)
            r[1] = float64(i)
            r[2] = float64(j)
            r[3] = f
            r[4] = float64(c)
            all = append(all, r)
            c++
        }
    }
}
sort.Sort(all)
#4
更多评论
#! /usr/bin/env python

def mul_nums(nums,size):
    curnums = []
    retnums = []
    minnum = nums[0]
    for c in nums:
        curnums.append(c)
        retnums.append(c)
        if c < minnum:
            minnum = c



    i = len(retnums)
    while i < size:
        minnum = curnums[0]
        minidx = 0
        idx = 0
        for c in curnums:
            if c < minnum:
                minnum = c
                minidx = idx
            idx += 1

        minmulnum = curnums[0] * nums[0]
        for c in curnums:
            for d in nums:
                if (c * d) < minmulnum:
                    minmulnum = (c * d)

        retnums.append(curnums[minidx])
        curnums[minidx] = minmulnum
        i += 1
    return retnums

def main():
    retnums = mul_nums([2,3,5],10000)
    for c in retnums:
        print('[%d]\n'%(c))
main()
#1
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
10000以内 素因子只有2,3,5的数
只需要log2(10000) * log3(10000)*log5(10000)次循环  520次 最终的数据个数小于这个数
"""
import math


def findOnly235():
    max = 10000
    maxL2 = int(math.log(max, 2))
    maxL3 = int(math.log(max, 3))
    maxL5 = int(math.log(max, 5))
    print maxL2, maxL3, maxL5
    L = []
    for i in range(maxL2):
        for j in range(maxL3):
            for k in range(maxL5):
                number = pow(2, i) * pow(3, j) * pow(5, k)
                if number <= max and number > 1:
                    L.append(number)

    return L

if __name__ == '__main__':
    l = findOnly235()
    l.sort()
    print l
#2