• 五分钟捡起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怎么开房不开方。

  • 相关阅读:
    Squirrel GUI+ Phoenix 连接Hbase
    Hadoop_Hbase集群完全离线安装[CDH 5.13.1]
    cmake生成Makefile时指定c/c++编译器
    一步一步搭建:spark之Standalone模式+zookeeper之HA机制
    linux 安装nginx
    string和json转换的简单应用
    RHEL7 添加用户,含sudo权限
    RHEL7 Ansible
    RHEL安装docker-compose
    博客园仿github的markdown样式
  • 原文地址:https://www.cnblogs.com/chihane/p/3460511.html
Copyright © 2020-2023  润新知