• 五分钟捡起Python妥妥的


     1 import math
     2 
     3 def primeFactor(source):
     4     start = int(math.floor(math.sqrt(source)))
     5     
     6     while start > 1:
     7         print start
     8         mod = source % start
     9         if mod == 0 and isPrime(start):
    10             return start
    11         start -= 1
    12         
    13 def isPrime(source):
    14     start = source / 2
    15     
    16     while start > 1:
    17         if source % start == 0:
    18             return False
    19         start -= 1
    20         
    21     return True
    22         
    23     
    24 print primeFactor(600851475143)
     1 import java.math.BigInteger;
     2 
     3 public class test {
     4     public static void main(String[] args) {
     5         System.out.println(primeFactor("600851475143"));
     6     }
     7     
     8     public static long primeFactor(String source) {
     9         BigInteger src = new BigInteger(source);
    10         long start = (long) src.divide(new BigInteger("2")).longValue();
    11         
    12         for (long number = start; number > 0; number--) {
    13             System.out.println(number);
    14             long module = src.mod(BigInteger.valueOf(number)).longValue();
    15             if (module == 0 && isPrime(number))
    16                 return number;
    17         }
    18         
    19         return -1;
    20     }
    21     
    22     public static boolean isPrime(long source) {
    23         long start = (long) Math.floor(source / 2);
    24         
    25         for (long number = start; number > 1; number--) {
    26             long module = source % number;
    27             if (module == 0)
    28                 return false;
    29         }
    30         return true;
    31     }
    32 }
    不妥

    前边的一分钟查忘记的语法,四分钟写代码,十秒出答案。

    后边的写了一个多小时现在还在算。谁告诉我BigInteger怎么开房不开方。

  • 相关阅读:
    day02_接口测试流程
    day01_接口测试常识丶HTTP协议
    day03_元素操作丶浏览器操作方法丶鼠标操作
    day05_数组
    day04_运算符
    day03_数据类型丶字符编码丶基本数据类型转换
    day02_注释丶关键字丶标识符丶常量丶变量
    day04_数据序列之字符串
    day03_流程控制语句
    day02_输入数据丶数据类型转换丶运算符
  • 原文地址:https://www.cnblogs.com/chihane/p/3460511.html
Copyright © 2020-2023  润新知