返回列表 发帖

[原创代码] 欧拉计划005-求最小同时都能被1-20整除的数

本帖最后由 codegay 于 2016-4-13 15:18 编辑

欧拉计划005-求最小同时都能被1-20整除的数
#=
julia解欧拉计划005-求最小同时都能被1-20整除的数
https://projecteuler.net/problem=5
=#
function ff1()
#= -_-!! 无脑暴力
以下表达式使用这样一行python生成然后复制粘贴过来的:
'&&'.join(["x%"+str(r)+"==0" for r in range(2,21)])
=#
f(x)=x%2==0&&x%3==0&&x%4==0&&x%5==0&&x%6==0&&x%7==0&&x%8==0&&x%9==0&&x%10==0&&x%11==0&&x%12==0&&x%13==0&&x%14==0&&x%15==0&&x%16==0&&x%17==0&&x%18==0&&x%19==0&&x%20==0;
    for r in countfrom(2)
        if f(r)
            println(r)
        break
        end
    end
end
@time ff1()
#=
232792560
  5.726657 seconds (232.85 M allocations: 3.472 GB, 2.59% gc time)
[Finished in 9.6s]
=#COPY
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

#julia 一行流
@time @show (2^4*3^2)*(*(primes(4,20)...))COPY
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 3# happy886rr


    一样的都是算最小公倍数。python也可以的。只是需要手动把质数都列出来。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 3# happy886rr


   
你可以玩玩J语言http://www.jsoftware.com/download/j804/install/

还有lua之类的。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

欧拉论坛里好多人贴出J的一行流:
*./>:i.20COPY
最短的是这样的。不明觉厉。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

"""
python
"""
from itertools import count
def ff1():
    #无脑暴力
    # ' and '.join(["x%"+str(r)+"==0" for r in range(2,21)])
    f=(lambda x:x%2==0 and x%3==0 and x%4==0 and x%5==0 and x%6==0 and x%7==0
and x%8==0 and x%9==0 and x%10==0 and x%11==0 and x%12==0 and x%13==0
and x%14==0 and x%15==0 and x%16==0 and x%17==0 and x%18==0 and x%19==0 and x%20==0)
    for r in count(1):
        if f(r):
            print(r)
            break
ff1()
"""
232792560
[Finished in 76.5s]
"""COPY
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 8# CrLf


    J语言
http://www.jsoftware.com/
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 10# happy886rr


  不要用is 判断等值。
https://segmentfault.com/q/1010000000150947
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 12# happy886rr


    但是还应该用==而不是is。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

返回列表