测试数组中的数字是否重复,然后将其删除

所以我的问题是我必须在一个数组中列出一个数字的素数因子,以及另一个素数因子在给定数组内相同位置上的幂(所以如果你想要 60 的素数因子,我需要返回一个内容如下的数组:素数:{2, 3, 5} 次幂 {2, 1, 1} => (2*2)*(3*1)*(5*1) = 60。我现在有以下代码来确定素数数组中的重复项,但是我现在如何不将它们打印到控制台,而是将它们保存在另一个变量中,然后将它们用于幂数组?


long current = primes[0];

boolean found = false;

for( int i = 0; i < primes.length; i++) {

   if( current == primes[i] && !found) {

      found = true;

   }

   else if( current != primes[i] ) {

      System.out.print(" " + current);

      current = primes[i];

      found = false;

   } 

完整的代码将是:


public class Algebra {


public static long [][] primfaktorzerlegung(long n){


    int position = 0;

    long[] primes = new long [0];

    long [] powers = new long [0];




    while(n%2 == 0) {

        primes[position] = 2;

        position++;

        n = n / 2;

    }

    for (int i = 3; i <= Math.sqrt(n); i+= 2)

    {

        while (n%i == 0)

        {


            n /= i;

        }

    }

    long current = primes[0];

    boolean found = false;

    for (int i = 0; i < primes.length; i++) {

        if (current == primes[i] && !found) {

            found = true;

        } else if (current != primes[i]) {

            current = primes[i];

            found = false;

        }



    }

    long[][] z = {primes,powers};

    return z;

    }

}

这显然是未完成的,但为了展示整个内容,我还是发布了它。


DIEA
浏览 131回答 2
2回答

12345678_0001

您想要每个素数的频率,并且在 java 中有一种标准方法可以做到这一点。此外,由于您不知道会有多少个素数,所以最好使用列表。但是您甚至不需要使用其中任何一个,只需使用 aMap<Long, Long>并在一次通过中累积素数和幂:Map<Long, Long> primePowers = new LinkedHashMap<>();for (int i = 2; i <= Math.sqrt(n); i+= 2) {&nbsp; &nbsp; while (n%i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; primePowers.put(i, primePowers.getOrDefault(i, 0L) + 1);&nbsp; &nbsp; &nbsp; &nbsp; n /= i;&nbsp; &nbsp; }}// convert the Map to the return valuelong[] primes, powers = new long[primePowers.size()];int i = 0;for (Map.Entry<Long, Long> entry : primePowers.entrySet()) {&nbsp; &nbsp; primes[i] = entry.getKey();&nbsp; &nbsp; powers[i] = entry.getValue();}return new long[][]{primes,powers};仅供参考 aLinkedHashMap以插入顺序迭代其条目。作为设计点,返回类型long[][]不是一个好的选择。任何两个数组必须就其元素对齐达成一致的情况都是糟糕的设计。

波斯汪

代码:import java.util.ArrayList;import java.util.List;public class Primes {&nbsp; &nbsp;static long getPowerOf( long power, long n, List<Long> primes, List<Long> powers ) {&nbsp; &nbsp; &nbsp; if(( n % power ) == 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;long count = 0L;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(( n % power ) == 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = n / power;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;primes.add( power );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;powers.add( count );&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return n;&nbsp; &nbsp;}&nbsp; &nbsp;static long[][] getPrimes( long n ) throws Exception {&nbsp; &nbsp; &nbsp; final List<Long> primes = new ArrayList<>();&nbsp; &nbsp; &nbsp; final List<Long> powers = new ArrayList<>();&nbsp; &nbsp; &nbsp; n = getPowerOf( 2, n, primes, powers );&nbsp; &nbsp; &nbsp; for( long i = 3; n > 1; i += 2 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;n = getPowerOf( i, n, primes, powers );&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if( n > 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new Exception( "More primes needed" );&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; final long[][] result = new long[2][];&nbsp; &nbsp; &nbsp; result[0] = new long[primes.size()];&nbsp; &nbsp; &nbsp; result[1] = new long[powers.size()];&nbsp; &nbsp; &nbsp; for( int i = 0; i < primes.size(); ++i ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result[0][i] = primes.get( i );&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; for( int i = 0; i < powers.size(); ++i ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result[1][i] = powers.get( i );&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp;}&nbsp; &nbsp;static void showPrimes( long[] primes, long[] powers ) {&nbsp; &nbsp; &nbsp; boolean tail = false;&nbsp; &nbsp; &nbsp; for( int i = 0; i < primes.length; ++i ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if( powers[i] > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( tail ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print( " + " );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tail = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print( primes[i] + "x" + powers[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp;}&nbsp; &nbsp;public static void main( String[] args ) throws Exception {&nbsp; &nbsp; &nbsp; final long[][] result = getPrimes( 2*2*3*5*5*7*23*23*23 );&nbsp; &nbsp; &nbsp; showPrimes( result[0], result[1] );&nbsp; &nbsp;}}输出:2x2 + 3x1 + 5x2 + 7x1 + 23x3笔记:使用类代替long[][]for primes 和 powers 会更好,如下所示:class PrimeUsage {&nbsp; &nbsp;long prime;&nbsp; &nbsp;long power;}PrimeUsage[] or List<PrimeUsage> or Set<PrimeUsage>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java