zoukankan      html  css  js  c++  java
  • 简单的数字查找

    0. Simple Number Finding

     

    You are playing a card game with your friends. This game in China named “扎金花”. In this game, the

    2, 3, 5 are some simple powerful numbers. Because the combination of 2,3,5 is less than any other combinations but greater than the AAA, which is the king in this game. In today, you want to find if a number is a simple number, in which their factors only include 2, 3 and 5.

    So your task is to find out whether a given number is an amazing number.

    E.g Input: 6

    Output: (2, 3)

    Explanation: 6 = 2 x 3

    Input: 8

    Output: (2, 2, 2)

    Explanation: 8 = 2 x 2 x 2

    Input: 14 Output:None

    Explanation: 14 is not amazing since it includes another prime factor 7.

    How to check your answer:

    If you test 1845281250, your program should give (2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5)

    If you test 3690562500, your program should give (2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5);

    If you test 1230187500, your program should give (2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5); If you test 10023750, your program should give None;

    from math import sqrt, floor
    
    
    def isPrime(number):
        flag = True
        factor = 0
        for i in range(2, floor(sqrt(number)) + 1):
            if number % i == 0:
                flag = False
                factor = i
                break
        return flag, factor
    
    
    if __name__ == "__main__":
        prompt = """请输入一个正整数:"""
        number = int(input(prompt))
        result = []
        while True:
            flag, factor = isPrime(number)
            if flag:
                if number in [2, 3, 5]:
                    result.append(number)
                else:
                    result = []
                break
            else:
                if factor in [2, 3, 5]:
                    result.append(factor)
                    number /= factor
                else:
                    result = []
                    break
        if result:
            print(tuple(result))
        else:
            print(None)
    

      

  • 相关阅读:
    Perl 基础笔记: 使用 cpanm 安装 Perl 模块
    修改CPAN安装源
    JQUERY实现点击INPUT使光标移动到最后或指定位置
    新手入门Underscore.js 中文(template)
    深入浅出C/C++中的正则表达式库
    [libxml2]_[XML处理]_[使用libxml2的xpath特性修改xml文件内容]
    Mysql事务的隔离级别
    HBase基础知识摘要
    java如何实现一个Future
    遇到过的问题整理-大量页面监控问题
  • 原文地址:https://www.cnblogs.com/keystone/p/10955208.html
Copyright © 2011-2022 走看看