5 Smallest multiple - Project Eulder
来源:互联网 发布:中国10月经济数据 编辑:程序博客网 时间:2024/06/05 08:53
package xxx.xxx.xxx;
import java.util.ArrayList;
/*
* 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?
*/
public class SmallestMultiple {
ArrayList<Integer> factors = new ArrayList<Integer>();
private void compute(int min, int max, int minMultiple) {
long multiple = (long) minMultiple;
ArrayList<Integer> dividers = new ArrayList<Integer>();
for (int num = min + 1; num < max + 1; num++) {
boolean isPrime = true;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
multiple = multiple * num;
// System.out.println(num);
}
else{
dividers.add(num);
}
}
// System.out.println(multiple);
for(int i = 0; i<dividers.size();i++){
if(multiple%dividers.get(i)!=0){
getAllFactors(dividers.get(i));
System.out.println("factors"+factors);
long temp = multiple;
for(int j = 0; j<factors.size();j++){
System.out.println("aaa"+factors.get(j));
if(temp%factors.get(j)==0){
temp = temp/factors.get(j);
}else{
multiple = multiple*factors.get(j);
}
}
}
}
System.out.println(multiple);
}
private void getAllFactors(int number) {
if (number > 1) {
for (int i = 2; i < number+1; i++) {
if(number%i==0){
// System.out.println(i);
factors.add(i);
this.getAllFactors(number/i);
break;
}
}
}
}
private void getTwoFactors(int number) {
if (number > 1) {
for (int i = 2; i < number+1; i++) {
if(number%i==0){
System.out.println(i);
System.out.println(number/i);
break;
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SmallestMultiple smallestMultiple = new SmallestMultiple();
smallestMultiple.compute(10, 20, 2520);
}
}
import java.util.ArrayList;
/*
* 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?
*/
public class SmallestMultiple {
ArrayList<Integer> factors = new ArrayList<Integer>();
private void compute(int min, int max, int minMultiple) {
long multiple = (long) minMultiple;
ArrayList<Integer> dividers = new ArrayList<Integer>();
for (int num = min + 1; num < max + 1; num++) {
boolean isPrime = true;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
multiple = multiple * num;
// System.out.println(num);
}
else{
dividers.add(num);
}
}
// System.out.println(multiple);
for(int i = 0; i<dividers.size();i++){
if(multiple%dividers.get(i)!=0){
getAllFactors(dividers.get(i));
System.out.println("factors"+factors);
long temp = multiple;
for(int j = 0; j<factors.size();j++){
System.out.println("aaa"+factors.get(j));
if(temp%factors.get(j)==0){
temp = temp/factors.get(j);
}else{
multiple = multiple*factors.get(j);
}
}
}
}
System.out.println(multiple);
}
private void getAllFactors(int number) {
if (number > 1) {
for (int i = 2; i < number+1; i++) {
if(number%i==0){
// System.out.println(i);
factors.add(i);
this.getAllFactors(number/i);
break;
}
}
}
}
private void getTwoFactors(int number) {
if (number > 1) {
for (int i = 2; i < number+1; i++) {
if(number%i==0){
System.out.println(i);
System.out.println(number/i);
break;
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SmallestMultiple smallestMultiple = new SmallestMultiple();
smallestMultiple.compute(10, 20, 2520);
}
}
0 0
- 5 Smallest multiple - Project Eulder
- 【Project Euler】【Problem 5】Smallest multiple
- project euler problem 5:Smallest multiple
- Project Euler Problem 5: Smallest multiple
- Project Euler 005 Smallest multiple
- Problem 5:Smallest multiple
- PE 5 Smallest multiple
- Problem 5 Smallest multiple
- (Problem 5)Smallest multiple
- 4 Largest palindrome product - Project Eulder
- Smallest multiple
- 欧拉计划(5) Smallest multiple
- Problem 5 Smallest multiple(LCM)
- Smallest Common Multiple
- Smallest Common Multiple
- Smallest Multiple by GoLang
- Smallest Common Multiple
- projecteuler---->problem=5----Smallest multiple n个数求最小公倍数
- html元素与间距
- 那些看着高大上的名词
- 自定义View画报表
- 优化SQL查询
- Android学习基本索引(一)——记录,没事自己看
- 5 Smallest multiple - Project Eulder
- 常见的设计模式
- java23种设计模式之装饰模式(Decorator)
- 在我程序员之路的开始写点东西吧
- Mac下如何统计一个目录下所有代码文件的总行数
- OC学习笔记 (10-15-18)
- 文章标题
- tomcat内存配置
- Remove Element