Martyr2项目实现——Number部分问题求解(3) Prime Factorization

质因子分解

问题描述:

Prime Factorization – Have the user enter a number and find all Prime Factors (if there are any) and display them.

翻译:

质因子分解:给定一个整数N,找到并输出他的全部质因子

原理:

质因数分解,是将一个正整数写成几个约数的成绩,并且这些约数都是质数

给定一个合数n(这里,n是待分解的正整数),试除法看成是用小于等于\(\sqrt{n}\)的每个素数去试除待分解的整数。如果找到一个数能够整除除尽,这个数就是待分解整数的因子。试除法一定能够找到n的因子。因为它检查n的所有可能的因子,所以如果这个算法“失败”,也就证明了n是个素数(参考wikipedia:试除法

算法实现:

public static ArrayList<Long> PrimeFactor(long N){ //使用短除法来找到一个合数的全部质因子
long k = (long)Math.sqrt(N);
ArrayList<Long> list = new ArrayList<>();
for(long i=2;i<=k;i++) {
while (N % i == 0) {
list.add(i);
N /= i;
}
}
if(N!=1) list.add(N);
return list; //返回质因子构成的列表
}

Martyr2项目实现——Number部分问题求解(3) Prime Factorization的更多相关文章

  1. Martyr2项目实现——Number部分的问题求解 (1) Find Pi to Nth Digit

    Martyr2项目实现--Number部分的问题求解 (1) Find Pi to Nth Digit Find Pi to Nth Digit 问题描述: Find PI to the Nth Di ...

  2. 最小公倍数 SRM 661 Div1 250: MissingLCM

    Problem Statement The least common multiple (denoted "lcm") of a non-empty sequence of pos ...

  3. about how to determine a prime number

    (1) if divided by 2 or 3, then no; (2) we only have to go through prime factors; because a composite ...

  4. 4_Is Prime

    4 // // ViewController.swift // Is Prime // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. All ...

  5. ural 1748 The Most Complex Number 和 丑数

    题目:http://acm.timus.ru/problem.aspx?space=1&num=1748 题意:求n范围内约数个数最多的那个数. Roughly speaking, for a ...

  6. Number Transformation

    Description In this problem, you are given a pair of integers A and B. You can transform any integer ...

  7. Almost Prime

    Description Almost Prime time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  8. USACO 1.5 Prime Palindromes

    Prime Palindromes The number 151 is a prime palindrome because it is both a prime number and a palin ...

  9. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

随机推荐

  1. 如何创建本地git分支到远程

    创建本地分支到远程: 1.$git init 之后创建的本地仓库默认master分支 如果现在就要$ git branch 查看当前分支,是不显示任何分支的,只有在add,commit文件之后才显示, ...

  2. hystrix总结之缓存

    通过实现HystrixCommand或者HystrixObservableCommand的getCacheKey方法,可以启动缓存. public class CommandUsingRequestC ...

  3. 使用Golang的singleflight防止缓存击穿

    背景 在使用缓存时,容易发生缓存击穿. 缓存击穿:一个存在的key,在缓存过期的瞬间,同时有大量的请求过来,造成所有请求都去读dB,这些请求都会击穿到DB,造成瞬时DB请求量大.压力骤增. singl ...

  4. stp域中两台switch互联接口出现两口均为root口 并且在现有stp区域中无法确定根桥设备位置;分析其原因并赋予解决办法

    stp域中两台switch互联接口出现两口均为root口 并且在现有stp区域中无法确定根桥设备位置:分析其原因并赋予解决办法 1.于上图描述了案例中当前组网环境的各交换机位置与stp状态情况  : ...

  5. Spring Cloud各组件学习

    Spring-Cloud 介绍 SpringCloud各个组件详解,因为SpringCloud部分组件停止更新,故本项目包含原SpringCloud(基于SpringCloud H版和SpringBo ...

  6. JVM学习(三)JVM垃圾回收

    一.引用的分类 在了解JVM垃圾回收机制之前,了解一下对象的引用类型是非常必要的. 强引用:GC时不会被回收 软引用:描述有用但不是必须的对象,在发生内存溢出异常之前被回收 弱引用:描述有用但不是必须 ...

  7. 解决 webpack .\src\main.js .\dist\bundle.js 错误

    打包的命令格式:webpack 要打包的文件的路径 打包好的输出文件的路径 栗子: webpack .\src\main.js .\dist\bundle.js 提示错误,错误信息如下: 错误原因 w ...

  8. Typora,你好!

    初识Typora 1.标题 一个井号+空格+回车 =一级标题 两个井号+空格+回车 =二级标题 三个井号+空格+回车 =三级标题 四个井号+空格+回车 =四级标题 快捷键的话: 按ctrl + 1 就 ...

  9. 使用implicitly demo

    泛型:  Context Bounds // //定义一个隐式值, 这个值不能少, 要不找不到比较的对象 implicit val personCompartor = new Ordering[Per ...

  10. Spring AOP系列(三) — 动态代理之JDK动态代理

    JDK动态代理 JDK动态代理核心是两个类:InvocationHandler和Proxy 举个栗子 为便于理解,首先看一个例子: 希望实现这样一个功能:使用UserService时,只需关注自己的核 ...