From 8f4d8664f426aaba2101bcf8efdd792960b4925c Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 22 Mar 2019 17:37:05 -0400 Subject: [PATCH] Fixed bug in getDivisors where number could be added to list twice --- mattrixwv/Algorithms.class | Bin 8840 -> 8918 bytes mattrixwv/Algorithms.java | 40 ++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class index 27e8ba61907f33c10a3cdc881cfc53216da60723..3ef991280dc1219d25ab47ed6b279a05d3cf71dd 100644 GIT binary patch delta 859 zcmZwGPiPZC6bA6`#cj4p)6_MMNlmnprifirwbiPHQawb9v;jRBEP}06@Svc0@?wrH z#ap7L(O}d{{D%aU?P{wQDiwu#l7c9x2SriPlZd3mH=EtWgY2@rnQvxx=f~_=bTL{u z<4tUa4ebD}fy5VZa~ie-Q&`sDrlBT_8nqw_DF?SI>9A{%M8G|?r5D2p&%K_^{BlqRv4t|Lx)9HtvMMYnO5?%+IU z*XSPd^Z*^amJjw~2NW5$6-)5Q*doK#9r^06NU5U)I=G@7;rop3Hk(y;vD9o!h|bpy>gW= zrT%L&#U>Bg;}Jac7(sf1R(gtcG>0yFhCZ6detM3B^a4leB~H@0HZFijfFgOf?l`yi&N2v4(o9Huo=nMMkEB4Vh4AOUu&<`Z&C&oBS(J!Rww}nxJmo2GT6vsbr-F4heZP(pxbS(@Mk{vY)&7iCh?3Ctht3woYD6ptY9fB63a*=Kt z>DHnhYKuBZ#X8WbIwVD%M2Lzy)S)Qq6#X-Mzj0dwnPGVU=KbFH^M12EnV!kh@$?pa zv;n{H>jOjg@$H$SY-b`zz?n?=Cs^+IUCIkssXXzL?Ieo9#Gp%sOVB0M8_-x^Yk|OE zOMx|`IW{GXcPvYzqlT3-o~EkCi8v;e4h$6An2dkF`9DAZz;OvXlRA!%b^7fl404eQ zxkROmQZ1K>$`u-9f@ZnOdbviITxXZuV7J_4pWNcG+~%al&dMD!a*qw#$EN%@)fT>R zWR9>!yOpN0{U?=lcxQDHWE-6eNF+?aLo)skNwPpBIO1_JW@sdoqhmWRsZ5uNgw9Wu>cdBP!i%29d7DS6HXjg83+IhkGL5>+m->N( getDivisors(int goalNumber){ + public static ArrayList getDivisors(Integer goalNumber){ ArrayList divisors = new ArrayList(); //Start by checking that the number is positive if(goalNumber <= 0){ @@ -410,16 +410,12 @@ public class Algorithms{ //If the number is 1 return just itself else if(goalNumber == 1){ divisors.add(1); - } - //Otherwise add 1 and itself to the list - else{ - divisors.add(1); - divisors.add(goalNumber); + return divisors; } //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly Double topPossibleDivisor = Math.ceil(Math.sqrt(goalNumber)); - for(Integer possibleDivisor = 2;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ + for(Integer possibleDivisor = 1;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ //If you find one add it and the number it creates to the list if((goalNumber % possibleDivisor) == 0){ divisors.add(possibleDivisor); @@ -427,6 +423,10 @@ public class Algorithms{ if(possibleDivisor != topPossibleDivisor.intValue()){ divisors.add(goalNumber / possibleDivisor); } + //Take care of a few occations where a number was added twice + if(divisors.get(divisors.size() - 1) == (possibleDivisor + 1)){ + ++possibleDivisor; + } } } @@ -444,23 +444,23 @@ public class Algorithms{ //If the number is 1 return just itself else if(goalNumber == 1){ divisors.add(1L); - } - //Otherwise add 1 and itself to the list - else{ - divisors.add(1L); - divisors.add(goalNumber); + return divisors; } //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly Double topPossibleDivisor = Math.ceil(Math.sqrt(goalNumber)); - for(Long possibleDivisor = 2L;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ + for(Long possibleDivisor = 1L;possibleDivisor <= topPossibleDivisor;++possibleDivisor){ //If you find one add it and the number it creates to the list if((goalNumber % possibleDivisor) == 0){ divisors.add(possibleDivisor); //Accound for the possibility of sqrt(goalNumber) being a divisor - if(possibleDivisor != topPossibleDivisor.intValue()){ + if(possibleDivisor != topPossibleDivisor.longValue()){ divisors.add(goalNumber / possibleDivisor); } + //Take care of a few occations where a number was added twice + if(divisors.get(divisors.size() - 1) == (possibleDivisor + 1L)){ + ++possibleDivisor; + } } } @@ -478,16 +478,12 @@ public class Algorithms{ //If the number is 1 return just itself else if(goalNumber.equals(BigInteger.valueOf(1))){ divisors.add(BigInteger.valueOf(1)); - } - //Otherwise add 1 and itself to the list - else{ - divisors.add(BigInteger.valueOf(1)); - divisors.add(goalNumber); + return divisors; } //Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly BigInteger topPossibleDivisor = goalNumber.sqrt(); - for(BigInteger possibleDivisor = BigInteger.valueOf(2);possibleDivisor.compareTo(topPossibleDivisor) <= 0;possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1))){ + for(BigInteger possibleDivisor = BigInteger.valueOf(1);possibleDivisor.compareTo(topPossibleDivisor) <= 0;possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1))){ //If you find one add it and the number it creates to the list if(goalNumber.mod(possibleDivisor).equals(BigInteger.valueOf(0))){ divisors.add(possibleDivisor); @@ -495,6 +491,10 @@ public class Algorithms{ if(!possibleDivisor.equals(topPossibleDivisor)){ divisors.add(goalNumber.divide(possibleDivisor)); } + //Take care of a few occations where a number was added twice + if(divisors.get(divisors.size() - 1).equals(possibleDivisor.add(BigInteger.valueOf(1L)))){ + possibleDivisor = possibleDivisor.add(BigInteger.valueOf(1)); + } } }