简体   繁体   English

如何在 Java 中乘以 1000 个素数

[英]How to multiply 1000 prime numbers in Java

I need to multiply around 1000 prime numbers in Java.我需要在 Java 中乘以大约 1000 个素数。 For example 2*3*5*7*11*13*17*19*....... Is it possible?例如 2*3*5*7*11*13*17*19*....... 可能吗? I tried with below one:我尝试了以下一个:

    BigInteger resultBig = BigInteger.valueOf(1);
    int n = 0;
    long result = 1;
    for(int i = 0; i < 100000; i++)
          {
         n = Prime(i);
         result = result * n;
         resultBig = new BigInteger(Integer.toString((int) result));
           }

As said in comments, you must do the math using BigInteger .正如评论中所说,您必须使用BigInteger进行数学运算。

final int[] primes = {
        2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
        // list abbreviated for clarity
        7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919
};

BigInteger result = BigInteger.valueOf(1);
for (int i = 0; i < 1000; i++)
    result = result.multiply(BigInteger.valueOf(primes[i]));
System.out.println(result);

Using the full list of 1000 primes found here , the result is:使用此处找到的 1000 个素数的完整列表,结果是:

678629608419755514953266004896957820972161078160377361970324401521111792080121479864721936071815069425907219215791646774510151130705671056416094404541167439287735488353736963531288441938981088407654256240451529081607242659988552012480001287133802278572298314458227654950008738955663072953766341488209509227159933381319371567666804963833249523370831655778314080604712246344649628072459805028063160913071005795183295590443375991860551286230065601580359306757988823124262933259305966372664091680948986620887898883461227980556352852601733860114246410887151983493540958775872577571329277597701163671587052591794386970584444752423596023268793021595936555282977008138833858707329536639661377014042325817639809356799596347944462538427778375525904007169834445567450156949173690701738594584875536885957881452438269676946038980597530032671949818526703398270502591574889228837327819994695664173214894557366363343168494592437205324652573516528943874382178600874878024643322031797588414862315122048846223291257900756812820806739795819803783834366449110996030165071920678407750230118672657378102915524688059208755108467225277065866103666795739208709483959119145497860116133180335757702319385020561042517429031288526721801002679092058170909635701703382390753126302005323612316630558515594616479515096004453718500060291836932140612551722161051067379805065002788004096547708243964735215852734827632098700684466036892770059458754742495711074949314613079781545359495019757827538184361308856825999513366660884541936335491466045305322353749545362962683762333460252556042583248154845846566948014188971651057314058851019340282646752239847232045463969939303431371658607220786663205842510175297602195433569758123945251755043878718459161595137019904240640962465899496512410906852088532419874383895656303779315512987369934711061777117329635461569528504994783413643047392160871963795694958724055597996525917454740621526108635321204763824742430011606570436994644169759611263012712375861911682673548369764923418748711813157811279361700331599397588282864147719911156923709896847720603482450047076226728760035577410722701184878333100234780537897462936378382079055966277885316116887834607362114802378706815302650083359076798475953780285866955566883261644281750278358349579977889429105626865087038835977930842352223971442123281019745568694318200865586150762549114357677130353514342849892002965601064686292493671204318349298134598116662388818407027989992498970986262856712232401426575229549744739851333516937170071337085705197690437625282926914858257689908846227286051735284322402597283976180484905838486513162987381659809287870592690902387482033879184700359561190209417618607868793293476867624464497838299321267571049753373623085351455438610076341961842557148160442782839736179329056237366708383637405663196770746783100179128651460773512143616414356080816160456447832856222804164147618891013658880373227849181446498052320436905124576367614898030410445386643656246089772967461562154147355201124738052009172637452710027640262529821855681129322547617443299372089380860873141895162966481252930360380537684913059090577224188204179681342669502124011214018434733385892140553307905100266308832521127607403573729242486985024795253305646999864066282626291530104297235324933472771821035277094700384260778312268190937365143307612108901729316774669077441981239149913617114331308200242717771235228048768133852203532299832810943137983635951570

Very Big Number: 3393 digits long.非常大的数字: 3393 位长。
Definitely larger than either int (<10 digits) or long (<19 digits) could manage.绝对大于int (<10 位)或long (<19 位)可以管理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM