the smallest positive number

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

 

程序比较好编,就是考验计算机。最大范围也是预估的,边写边调。

尽管算对了,但方式可能不对。

def div2(n,x):
    isDiv = True
    for i in xrange(2,n):
        if x % i != 0 :
            isDiv = False
            break
    return isDiv
for i in xrange(2,1772146456):
    if div2(21,i) == True:
        print i
        break

输出:

C:\webpy\webpy\Scripts\python.exe C:/pycode/euler.py
232792560

Process finished with exit code 0

后来请高手重新写了一个,这个就很正规了。

def getSmallestNum(m,n):
    for j in range(1,n):
        if( m*j%n ==0 ):
            return m*j
    return m*n

smallestNum = 1
for i in range(2,21):
    if(smallestNum%i !=0):
        smallestNum = getSmallestNum(smallestNum,i)

print smallestNum

 

时间: 2024-08-08 07:15:25

the smallest positive number的相关文章

从如何判断浮点数是否等于0说起——浮点数的机器级表示

题目中针对的0,对于浮点类型,具体指的是0.0,自然对于指针类型就是NULL,对于整型就是0,一些常见笔试面试题中常出现,不要较真,十分欢迎提出改进意见. 本文很大程度上收到林锐博士一些文章的启发,lz也是在大学期间读过,感觉收益良多,但是当时林锐也是说了结论,lz也只是知其然,而不知其所以然,为什么要那样写?为什么要这样用?往往一深究起来就稀里糊涂了,现在有幸还是继续读书,我发现了很多问题理解的还不透彻,亡羊补牢. 比如:有int d;  int *d; bool d; double d:几个

【Project Euler】5 第五题

 //2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. //What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?         static void Main(string[] args

oracle中dump函数及oracle NUMBER类型内部存储机制

oracle中dump函数 转自:http://blog.vsharing.com/nimrod/A654847.html DUMP函数的输出格式类似: 类型 ,符号/指数位 [数字1,数字2,数字3,......,数字20] 各位的含义如下: 1.类型: Number型,Type=2 (类型代码可以从Oracle的文档上查到) 2.长度:指存储的字节数 3.符号/指数位 在存储上,Oracle对正数和负数分别进行存储转换: 正数:加1存储(为了避免Null)负数:被101减,如果总长度小于21

UVa 701 The Archeologists' Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

CodeForces 233B Non-square Equation

链接: http://codeforces.com/problemset/problem/233/B 题目: B. Non-square Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's consider equation: x2+s(x)·x-n=0, where x,n are positive

UVa 188:Perfect Hash

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=124 类型: 哈希 原题: Perfect Software, Inc. has obtained a government contract to examine text flowing through a high-speed network

BigDecimal

/* * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ /* * Portions Copyright IBM Corporation, 2001. All Rights

Problem23

1.A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. 2. 3.A number n is ca

Matlab编程基础

原文:Matlab编程基础   平台:Win7 64 bit,Matlab R2014a(8.3)   "Matlab"是"Matrix Laboratory" 的缩写,中文"矩阵实验室",是强大的数学工具.本文侧重于Matlab的编程语言侧面,讲述Matlab的基本语法,以及用Matlab语言进行程序设计.值得一提的是,Matlab从R2014a版本开始支持中文语言了!   1.基本概念 Matlab默认启动后界面: Matlab有关的文件后缀