求这段大数阶乘算法的时间复杂度和空间复杂度

发布于 2022-09-11 22:18:02 字数 1147 浏览 20 评论 0

利用bigInteger实现了一下大数阶乘的算法,实测比直接迭代相乘快一个数量级,但是不知道怎么求时间和空间复杂,想大家帮忙看下。

private static String factorial(int n) {
        java.math.BigInteger ans = java.math.BigInteger.valueOf(1);
        int bits = 0;
        for (int i = 2; i <= n; i++) {
            if (Integer.bitCount(i) == 1) {
                bits += Integer.numberOfTrailingZeros(i);
                continue;
            }
//            ans = ans.multiply(java.math.BigInteger.valueOf(i));
        }
        ans = subFactorial(1, n);

        return ans.shiftLeft(bits).toString();
    }

    private static java.math.BigInteger subFactorial(long a, long b) {
        if ((b - a) < 10) {
            java.math.BigInteger res = java.math.BigInteger.ONE;
            for (long i = a; i <= b; i++) {
                if (Long.bitCount(i) == 1) {
                    continue;
                }
                res = res.multiply(java.math.BigInteger.valueOf(i));
            }
            return res;
        } else {
            long mid = a + b >>> 1;
            return subFactorial(a, mid).multiply(subFactorial(mid + 1, b));
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛放△進行李 2022-09-18 22:18:02

教你个简单粗暴的办法 - 计数法:

请输入package cn.llzg.opinions.utils;

public class Complecity {
    static long count=0;
    private static String factorial(int n) {
        java.math.BigInteger ans = java.math.BigInteger.valueOf(1);
        int bits = 0;
        for (int i = 2; i <= n; i++) {
            count++;
            if (Integer.bitCount(i) == 1) {
                bits += Integer.numberOfTrailingZeros(i);
                continue;
            }
        }
        ans = subFactorial(1, n);

        return ans.shiftLeft(bits).toString();
    }

    private static java.math.BigInteger subFactorial(long a, long b) {
        if ((b - a) < 10) {
            java.math.BigInteger res = java.math.BigInteger.ONE;
            for (long i = a; i <= b; i++) {
                count++;
                if (Long.bitCount(i) == 1) {
                    continue;
                }
                res = res.multiply(java.math.BigInteger.valueOf(i));
            }
            return res;
        } else {
            long mid = a + b >>> 1;
            return subFactorial(a, mid).multiply(subFactorial(mid + 1, b));
        }
    }

    public static void main(String[] args) {
        for(int i=1; i<100;i++){
            count=0;
            System.out.println("fac="+factorial(i));
            System.out.println("cnt="+count+" i="+i+" count/i="+((count+0.0)/i));
        }
    }
}

输出

/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=64903:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/xwx/IdeaProjects/opinions/src/main/webapp/WEB-INF/classes:/Users/xwx/.m2/repository/org/springframework/spring-core/5.0.9.RELEASE/spring-core-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-jcl/5.0.9.RELEASE/spring-jcl-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-beans/5.0.9.RELEASE/spring-beans-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-context/5.0.9.RELEASE/spring-context-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-aop/5.0.9.RELEASE/spring-aop-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-context-support/5.0.9.RELEASE/spring-context-support-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-jdbc/5.0.9.RELEASE/spring-jdbc-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-tx/5.0.9.RELEASE/spring-tx-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-expression/5.0.9.RELEASE/spring-expression-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-web/5.0.9.RELEASE/spring-web-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-webmvc/5.0.9.RELEASE/spring-webmvc-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-orm/5.0.9.RELEASE/spring-orm-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-oxm/5.0.9.RELEASE/spring-oxm-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-jms/5.0.9.RELEASE/spring-jms-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-messaging/5.0.9.RELEASE/spring-messaging-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/data/spring-data-solr/4.0.0.RELEASE/spring-data-solr-4.0.0.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/data/spring-data-commons/2.1.0.RELEASE/spring-data-commons-2.1.0.RELEASE.jar:/Users/xwx/.m2/repository/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar:/Users/xwx/.m2/repository/org/apache/solr/solr-solrj/7.7.1/solr-solrj-7.7.1.jar:/Users/xwx/.m2/repository/commons-io/commons-io/2.5/commons-io-2.5.jar:/Users/xwx/.m2/repository/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar:/Users/xwx/.m2/repository/org/apache/zookeeper/zookeeper/3.4.13/zookeeper-3.4.13.jar:/Users/xwx/.m2/repository/org/codehaus/woodstox/stax2-api/3.1.4/stax2-api-3.1.4.jar:/Users/xwx/.m2/repository/org/codehaus/woodstox/woodstox-core-asl/4.4.1/woodstox-core-asl-4.4.1.jar:/Users/xwx/.m2/repository/org/noggit/noggit/0.8/noggit-0.8.jar:/Users/xwx/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.24/jcl-over-slf4j-1.7.24.jar:/Users/xwx/.m2/repository/org/springframework/security/spring-security-web/4.2.5.RELEASE/spring-security-web-4.2.5.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/security/spring-security-core/4.2.5.RELEASE/spring-security-core-4.2.5.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/security/spring-security-config/4.2.5.RELEASE/spring-security-config-4.2.5.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/spring-test/5.0.9.RELEASE/spring-test-5.0.9.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/security/spring-security-taglibs/4.2.5.RELEASE/spring-security-taglibs-4.2.5.RELEASE.jar:/Users/xwx/.m2/repository/org/springframework/security/spring-security-acl/4.2.5.RELEASE/spring-security-acl-4.2.5.RELEASE.jar:/Users/xwx/.m2/repository/com/cycnet/cycnet-tools/0.3.7/cycnet-tools-0.3.7.jar:/Users/xwx/.m2/repository/commons-dbutils/commons-dbutils/1.1/commons-dbutils-1.1.jar:/Users/xwx/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/Users/xwx/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar:/Users/xwx/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/xwx/.m2/repository/commons-configuration/commons-configuration-zookeeper/1.2-SNAPSHOT/commons-configuration-zookeeper-1.2-20190219.095641-2.jar:/Users/xwx/.m2/repository/org/apache/curator/curator-recipes/2.7.1/curator-recipes-2.7.1.jar:/Users/xwx/.m2/repository/org/apache/commons/commons-configuration2/2.2/commons-configuration2-2.2.jar:/Users/xwx/.m2/repository/com/cycnet/cycnet-jmesa/0.2.5/cycnet-jmesa-0.2.5.jar:/Users/xwx/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/Users/xwx/.m2/repository/com/foundationdb/fdb-sql-parser/1.2.0/fdb-sql-parser-1.2.0.jar:/Users/xwx/.m2/repository/com/cycnet/cycnet-dbtools/0.8.8/cycnet-dbtools-0.8.8.jar:/Users/xwx/.m2/repository/commons-beanutils/commons-beanutils/1.9.3/commons-beanutils-1.9.3.jar:/Users/xwx/.m2/repository/com/google/guava/guava/19.0/guava-19.0.jar:/Users/xwx/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/xwx/.m2/repository/cn/llzg/gendata/0.1.1/gendata-0.1.1.jar:/Users/xwx/.m2/repository/com/bertramlabs/plugins/asset-pipeline-grails/2.14.2/asset-pipeline-grails-2.14.2.jar:/Users/xwx/.m2/repository/com/bertramlabs/plugins/asset-pipeline-core/2.14.2/asset-pipeline-core-2.14.2.jar:/Users/xwx/.m2/repository/org/jmesa/jmesa/4.0.1/jmesa-4.0.1.jar:/Users/xwx/.m2/repository/commons-el/commons-el/1.0/commons-el-1.0.jar:/Users/xwx/.m2/repository/rhino/js/1.7R2/js-1.7R2.jar:/Users/xwx/.m2/repository/bsf/bsf/2.4.0/bsf-2.4.0.jar:/Users/xwx/.m2/repository/net/sourceforge/jexcelapi/jxl/2.6.10/jxl-2.6.10.jar:/Users/xwx/.m2/repository/org/apache/poi/poi-ooxml/3.10-FINAL/poi-ooxml-3.10-FINAL.jar:/Users/xwx/.m2/repository/org/apache/poi/poi-ooxml-schemas/3.10-FINAL/poi-ooxml-schemas-3.10-FINAL.jar:/Users/xwx/.m2/repository/org/apache/xmlbeans/xmlbeans/2.3.0/xmlbeans-2.3.0.jar:/Users/xwx/.m2/repository/com/lowagie/itext/2.1.3/itext-2.1.3.jar:/Users/xwx/.m2/repository/bouncycastle/bcmail-jdk14/138/bcmail-jdk14-138.jar:/Users/xwx/.m2/repository/bouncycastle/bcprov-jdk14/138/bcprov-jdk14-138.jar:/Users/xwx/.m2/repository/org/ccil/cowan/tagsoup/tagsoup/1.1.3/tagsoup-1.1.3.jar:/Users/xwx/.m2/repository/com/opensymphony/xwork/2.0.5/xwork-2.0.5.jar:/Users/xwx/.m2/repository/opensymphony/ognl/2.6.11/ognl-2.6.11.jar:/Users/xwx/.m2/repository/com/google/code/gson/gson/1.7.1/gson-1.7.1.jar:/Users/xwx/.m2/repository/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar:/Users/xwx/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/xwx/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/xwx/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/xwx/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar:/Users/xwx/.m2/repository/commons-fileupload/commons-fileupload/1.2.2/commons-fileupload-1.2.2.jar:/Users/xwx/.m2/repository/com/cycnet/cycnet-cache/0.0.8/cycnet-cache-0.0.8.jar:/Users/xwx/.m2/repository/org/jgroups/jgroups/2.12.3.Final/jgroups-2.12.3.Final.jar:/Users/xwx/.m2/repository/opensymphony/oscache/2.4.1/oscache-2.4.1.jar:/Users/xwx/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar:/Users/xwx/.m2/repository/org/hibernate/hibernate-validator/5.4.1.Final/hibernate-validator-5.4.1.Final.jar:/Users/xwx/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar:/Users/xwx/.m2/repository/com/fasterxml/classmate/1.3.1/classmate-1.3.1.jar:/Users/xwx/.m2/repository/javax/mail/mail/1.4.5/mail-1.4.5.jar:/Users/xwx/.m2/repository/javax/activation/activation/1.1/activation-1.1.jar:/Users/xwx/.m2/repository/commons-validator/commons-validator/1.4.0/commons-validator-1.4.0.jar:/Users/xwx/.m2/repository/commons-digester/commons-digester/1.8/commons-digester-1.8.jar:/Users/xwx/.m2/repository/opensymphony/sitemesh/2.4.2/sitemesh-2.4.2.jar:/Users/xwx/.m2/repository/org/quartz-scheduler/quartz/2.2.3/quartz-2.2.3.jar:/Users/xwx/.m2/repository/c3p0/c3p0/0.9.1.1/c3p0-0.9.1.1.jar:/Users/xwx/.m2/repository/org/quartz-scheduler/quartz-jobs/2.2.3/quartz-jobs-2.2.3.jar:/Users/xwx/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.6/jackson-databind-2.9.6.jar:/Users/xwx/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/Users/xwx/.m2/repository/com/alibaba/fastjson/1.2.49/fastjson-1.2.49.jar:/Users/xwx/.m2/repository/com/ssll/ssllutils/0.3.1/ssllutils-0.3.1.jar:/Users/xwx/.m2/repository/net/sourceforge/pinyin4j/2.5.0/pinyin4j-2.5.0.jar:/Users/xwx/.m2/repository/org/apache/httpcomponents/fluent-hc/4.5.6/fluent-hc-4.5.6.jar:/Users/xwx/.m2/repository/com/qq/opensns/sdk4j/2.0/sdk4j-2.0.jar:/Users/xwx/.m2/repository/org/apache/httpcomponents/httpclient/4.5.6/httpclient-4.5.6.jar:/Users/xwx/.m2/repository/org/apache/httpcomponents/httpcore/4.4.10/httpcore-4.4.10.jar:/Users/xwx/.m2/repository/org/apache/httpcomponents/httpmime/4.5.6/httpmime-4.5.6.jar:/Users/xwx/.m2/repository/org/jsoup/jsoup/1.8.3/jsoup-1.8.3.jar:/Users/xwx/.m2/repository/net/sourceforge/htmlunit/htmlunit/1.14/htmlunit-1.14.jar:/Users/xwx/.m2/repository/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1.jar:/Users/xwx/.m2/repository/xerces/xmlParserAPIs/2.6.2/xmlParserAPIs-2.6.2.jar:/Users/xwx/.m2/repository/nekohtml/nekohtml/0.9.5/nekohtml-0.9.5.jar:/Users/xwx/.m2/repository/net/sourceforge/cssparser/cssparser/0.9.4/cssparser-0.9.4.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-java/2.28.0/selenium-java-2.28.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-android-driver/2.28.0/selenium-android-driver-2.28.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-remote-driver/2.28.0/selenium-remote-driver-2.28.0.jar:/Users/xwx/.m2/repository/cglib/cglib-nodep/2.1_3/cglib-nodep-2.1_3.jar:/Users/xwx/.m2/repository/org/json/json/20080701/json-20080701.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-chrome-driver/2.28.0/selenium-chrome-driver-2.28.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-htmlunit-driver/2.28.0/selenium-htmlunit-driver-2.28.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-api/2.28.0/selenium-api-2.28.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-firefox-driver/2.28.0/selenium-firefox-driver-2.28.0.jar:/Users/xwx/.m2/repository/org/apache/commons/commons-exec/1.1/commons-exec-1.1.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-ie-driver/2.28.0/selenium-ie-driver-2.28.0.jar:/Users/xwx/.m2/repository/net/java/dev/jna/jna/3.4.0/jna-3.4.0.jar:/Users/xwx/.m2/repository/net/java/dev/jna/platform/3.4.0/platform-3.4.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-iphone-driver/2.28.0/selenium-iphone-driver-2.28.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-safari-driver/2.28.0/selenium-safari-driver-2.28.0.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-support/2.28.0/selenium-support-2.28.0.jar:/Users/xwx/.m2/repository/org/webbitserver/webbit/0.4.14/webbit-0.4.14.jar:/Users/xwx/.m2/repository/io/netty/netty/3.5.2.Final/netty-3.5.2.Final.jar:/Users/xwx/.m2/repository/org/dbunit/dbunit/2.4.9/dbunit-2.4.9.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/selenium-server/2.31.0/selenium-server-2.31.0.jar:/Users/xwx/.m2/repository/org/bouncycastle/bcprov-jdk15on/1.48/bcprov-jdk15on-1.48.jar:/Users/xwx/.m2/repository/org/bouncycastle/bcpkix-jdk15on/1.48/bcpkix-jdk15on-1.48.jar:/Users/xwx/.m2/repository/mx4j/mx4j-tools/3.0.1/mx4j-tools-3.0.1.jar:/Users/xwx/.m2/repository/org/mortbay/jetty/servlet-api-2.5/6.1.9/servlet-api-2.5-6.1.9.jar:/Users/xwx/.m2/repository/org/seleniumhq/selenium/jetty-repacked/7.6.1/jetty-repacked-7.6.1.jar:/Users/xwx/.m2/repository/net/jcip/jcip-annotations/1.0/jcip-annotations-1.0.jar:/Users/xwx/.m2/repository/org/yaml/snakeyaml/1.8/snakeyaml-1.8.jar:/Users/xwx/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar:/Users/xwx/.m2/repository/net/sf/ehcache/ehcache/2.10.4/ehcache-2.10.4.jar:/Users/xwx/.m2/repository/org/glassfish/jersey/containers/jersey-container-servlet/2.27/jersey-container-servlet-2.27.jar:/Users/xwx/.m2/repository/org/glassfish/jersey/containers/jersey-container-servlet-core/2.27/jersey-container-servlet-core-2.27.jar:/Users/xwx/.m2/repository/org/glassfish/hk2/external/javax.inject/2.5.0-b42/javax.inject-2.5.0-b42.jar:/Users/xwx/.m2/repository/org/glassfish/jersey/core/jersey-common/2.27/jersey-common-2.27.jar:/Users/xwx/.m2/repository/javax/annotation/javax.annotation-api/1.2/javax.annotation-api-1.2.jar:/Users/xwx/.m2/repository/org/glassfish/hk2/osgi-resource-locator/1.0.1/osgi-resource-locator-1.0.1.jar:/Users/xwx/.m2/repository/org/glassfish/jersey/core/jersey-server/2.27/jersey-server-2.27.jar:/Users/xwx/.m2/repository/org/glassfish/jersey/core/jersey-client/2.27/jersey-client-2.27.jar:/Users/xwx/.m2/repository/org/glassfish/jersey/media/jersey-media-jaxb/2.27/jersey-media-jaxb-2.27.jar:/Users/xwx/.m2/repository/javax/ws/rs/javax.ws.rs-api/2.1/javax.ws.rs-api-2.1.jar:/Users/xwx/.m2/repository/org/glassfish/jersey/inject/jersey-hk2/2.27/jersey-hk2-2.27.jar:/Users/xwx/.m2/repository/org/glassfish/hk2/hk2-locator/2.5.0-b42/hk2-locator-2.5.0-b42.jar:/Users/xwx/.m2/repository/org/glassfish/hk2/external/aopalliance-repackaged/2.5.0-b42/aopalliance-repackaged-2.5.0-b42.jar:/Users/xwx/.m2/repository/org/glassfish/hk2/hk2-api/2.5.0-b42/hk2-api-2.5.0-b42.jar:/Users/xwx/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/Users/xwx/.m2/repository/org/glassfish/hk2/hk2-utils/2.5.0-b42/hk2-utils-2.5.0-b42.jar:/Users/xwx/.m2/repository/javax/servlet/jstl/1.2/jstl-1.2.jar:/Users/xwx/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/xwx/.m2/repository/javax/el/javax.el-api/2.2.4/javax.el-api-2.2.4.jar:/Users/xwx/.m2/repository/org/glassfish/web/javax.el/2.2.4/javax.el-2.2.4.jar:/Users/xwx/.m2/repository/com/ssll/local-iserver/0.1.2-SNAPSHOT/local-iserver-0.1.2-20190810.145017-1.jar:/Users/xwx/.m2/repository/org/codehaus/jackson/jackson-core-asl/1.9.6/jackson-core-asl-1.9.6.jar:/Users/xwx/.m2/repository/org/codehaus/jackson/jackson-mapper-asl/1.9.6/jackson-mapper-asl-1.9.6.jar:/Users/xwx/.m2/repository/commons-configuration/commons-configuration/1.6/commons-configuration-1.6.jar:/Users/xwx/.m2/repository/com/google/zxing/core/2.3-SNAPSHOT/core-2.3-20130816.012539-1.jar:/Users/xwx/.m2/repository/im4java/im4java/1.4.0/im4java-1.4.0-1.5.jar:/Users/xwx/.m2/repository/com/sun/media/jai_imageio/1.1/jai_imageio-1.1.jar:/Users/xwx/.m2/repository/javax/media/jai_core/1.1.2/jai_core-1.1.2.jar:/Users/xwx/.m2/repository/com/sun/media/jai_codec/1.1.0/jai_codec-1.1.0.jar:/Users/xwx/.m2/repository/jmagick/jmagick/6.3.9/jmagick-6.3.9.jar:/Users/xwx/.m2/repository/openmap/openmap/openmap/openmap-openmap.jar:/Users/xwx/.m2/repository/JimiProClasses/JimiProClasses/1.4/JimiProClasses-1.4.jar:/Users/xwx/.m2/repository/org/apache/ant.tools.zip/1.9.2/ant.tools.zip-1.9.2.jar:/Users/xwx/.m2/repository/com/thetransactioncompany/cors-filter/1.5/cors-filter-1.5.jar:/Users/xwx/.m2/repository/javax/jms/jms/1.1/jms-1.1.jar:/Users/xwx/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.10/jackson-core-2.8.10.jar:/Users/xwx/.m2/repository/com/octo/captcha/jcaptcha-api/1.0/jcaptcha-api-1.0.jar:/Users/xwx/.m2/repository/qqwry/qqwry/2015.4.1/qqwry-2015.4.1.jar:/Users/xwx/.m2/repository/com/octo/captcha/jcaptcha/1.0.1/jcaptcha-1.0.1-jdk7.jar:/Users/xwx/.m2/repository/com/jhlabs/imaging/01012005/imaging-01012005.jar:/Users/xwx/.m2/repository/org/apache/poi/poi/3.10.1/poi-3.10.1.jar:/Users/xwx/.m2/repository/org/springframework/mobile/spring-mobile-device/1.1.3.RELEASE/spring-mobile-device-1.1.3.RELEASE.jar:/Users/xwx/.m2/repository/org/freemarker/freemarker/2.3.23/freemarker-2.3.23.jar:/Users/xwx/.m2/repository/javax/transaction/jta/1.1/jta-1.1.jar:/Users/xwx/.m2/repository/org/jdom/jdom2/2.0.6/jdom2-2.0.6.jar:/Users/xwx/.m2/repository/jaxen/jaxen/1.1.6/jaxen-1.1.6.jar:/Users/xwx/.m2/repository/com/alipay/alipay-sdk-java/20170829/alipay-sdk-java-20170829.jar:/Users/xwx/.m2/repository/edu/uci/ics/crawler4j/4.4.0/crawler4j-4.4.0.jar:/Users/xwx/.m2/repository/com/sleepycat/je/5.0.84/je-5.0.84.jar:/Users/xwx/.m2/repository/org/apache/tika/tika-parsers/1.16/tika-parsers-1.16.jar:/Users/xwx/.m2/repository/org/apache/james/apache-mime4j-core/0.8.1/apache-mime4j-core-0.8.1.jar:/Users/xwx/.m2/repository/org/apache/james/apache-mime4j-dom/0.8.1/apache-mime4j-dom-0.8.1.jar:/Users/xwx/.m2/repository/org/slf4j/jul-to-slf4j/1.7.24/jul-to-slf4j-1.7.24.jar:/Users/xwx/.m2/repository/com/gravity/goose/2.1.23/goose-2.1.23.jar:/Users/xwx/.m2/repository/org/scala-lang/scala-compiler/2.9.0-1/scala-compiler-2.9.0-1.jar:/Users/xwx/.m2/repository/org/scala-lang/scala-library/2.9.0-1/scala-library-2.9.0-1.jar:/Users/xwx/.m2/repository/it/unimi/dsi/dsiutils/2.0.15/dsiutils-2.0.15.jar:/Users/xwx/.m2/repository/com/martiansoftware/jsap/2.1/jsap-2.1.jar:/Users/xwx/.m2/repository/org/mozilla/chardet/1.1/chardet-1.1.jar:/Users/xwx/.m2/repository/org/apache/curator/curator-framework/4.0.1/curator-framework-4.0.1.jar:/Users/xwx/.m2/repository/org/apache/curator/curator-client/4.0.1/curator-client-4.0.1.jar:/Users/xwx/.m2/repository/us/codecraft/xsoup/0.3.1/xsoup-0.3.1.jar:/Users/xwx/.m2/repository/org/assertj/assertj-core/1.5.0/assertj-core-1.5.0.jar:/Users/xwx/.m2/repository/joda-time/joda-time/2.9.9/joda-time-2.9.9.jar:/Users/xwx/.m2/repository/org/apache/tika/tika-langdetect/1.18/tika-langdetect-1.18.jar:/Users/xwx/.m2/repository/org/apache/tika/tika-core/1.18/tika-core-1.18.jar:/Users/xwx/.m2/repository/com/optimaize/languagedetector/language-detector/0.6/language-detector-0.6.jar:/Users/xwx/.m2/repository/net/arnx/jsonic/1.2.11/jsonic-1.2.11.jar:/Users/xwx/.m2/repository/com/intellij/annotations/12.0/annotations-12.0.jar:/Users/xwx/.m2/repository/org/apache/cxf/cxf-rt-rs-client/3.0.16/cxf-rt-rs-client-3.0.16.jar:/Users/xwx/.m2/repository/org/apache/cxf/cxf-rt-transports-http/3.0.16/cxf-rt-transports-http-3.0.16.jar:/Users/xwx/.m2/repository/org/apache/cxf/cxf-core/3.0.16/cxf-core-3.0.16.jar:/Users/xwx/.m2/repository/org/apache/ws/xmlschema/xmlschema-core/2.2.2/xmlschema-core-2.2.2.jar:/Users/xwx/.m2/repository/org/apache/cxf/cxf-rt-frontend-jaxrs/3.0.16/cxf-rt-frontend-jaxrs-3.0.16.jar:/Users/xwx/.m2/repository/xerces/xercesImpl/2.12.0/xercesImpl-2.12.0.jar:/Users/xwx/.m2/repository/xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar:/Users/xwx/.m2/repository/it/unimi/dsi/fastutil/8.2.2/fastutil-8.2.2.jar:/Users/xwx/.m2/repository/com/kennycason/kumo-core/1.17/kumo-core-1.17.jar:/Users/xwx/.m2/repository/com/kennycason/kumo-api/1.17/kumo-api-1.17.jar:/Users/xwx/.m2/repository/com/github/davidmoten/rtree/0.8.6/rtree-0.8.6.jar:/Users/xwx/.m2/repository/com/github/davidmoten/guava-mini/0.1.1/guava-mini-0.1.1.jar:/Users/xwx/.m2/repository/io/reactivex/rxjava/1.3.8/rxjava-1.3.8.jar:/Users/xwx/.m2/repository/com/kennycason/kumo-tokenizers/1.17/kumo-tokenizers-1.17.jar:/Users/xwx/.m2/repository/org/languagetool/language-en/2.5/language-en-2.5.jar:/Users/xwx/.m2/repository/org/languagetool/languagetool-core/2.5/languagetool-core-2.5.jar:/Users/xwx/.m2/repository/org/carrot2/morfologik-fsa/1.9.0/morfologik-fsa-1.9.0.jar:/Users/xwx/.m2/repository/org/carrot2/morfologik-speller/1.9.0/morfologik-speller-1.9.0.jar:/Users/xwx/.m2/repository/org/carrot2/morfologik-stemming/1.9.0/morfologik-stemming-1.9.0.jar:/Users/xwx/.m2/repository/net/sourceforge/segment/segment/1.4.2/segment-1.4.2.jar:/Users/xwx/.m2/repository/org/apache/opennlp/opennlp-tools/1.5.3/opennlp-tools-1.5.3.jar:/Users/xwx/.m2/repository/org/apache/opennlp/opennlp-maxent/3.0.3/opennlp-maxent-3.0.3.jar:/Users/xwx/.m2/repository/edu/washington/cs/knowitall/opennlp-tokenize-models/1.5/opennlp-tokenize-models-1.5.jar:/Users/xwx/.m2/repository/edu/washington/cs/knowitall/opennlp-postag-models/1.5/opennlp-postag-models-1.5.jar:/Users/xwx/.m2/repository/edu/washington/cs/knowitall/opennlp-chunk-models/1.5/opennlp-chunk-models-1.5.jar:/Users/xwx/.m2/repository/org/languagetool/language-zh/2.5/language-zh-2.5.jar:/Users/xwx/.m2/repository/com/google/code/cjftransform/1.0.1/cjftransform-1.0.1.jar:/Users/xwx/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar:/Users/xwx/.m2/repository/org/hibernate/hibernate-core/4.1.0.Final/hibernate-core-4.1.0.Final.jar:/Users/xwx/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar:/Users/xwx/.m2/repository/org/jboss/spec/javax/transaction/jboss-transaction-api_1.1_spec/1.0.0.Final/jboss-transaction-api_1.1_spec-1.0.0.Final.jar:/Users/xwx/.m2/repository/org/javassist/javassist/3.15.0-GA/javassist-3.15.0-GA.jar:/Users/xwx/.m2/repository/org/hibernate/common/hibernate-commons-annotations/4.0.1.Final/hibernate-commons-annotations-4.0.1.Final.jar:/Users/xwx/.m2/repository/com/hankcs/hanlp/portable-1.7.0/hanlp-portable-1.7.0.jar:/Users/xwx/.m2/repository/com/googlecode/ictclas4j/ictclas4j/1.1.0-SNAPSHOT/ictclas4j-1.1.0-20190301.114113-1.jar:/Users/xwx/.m2/repository/org/mockito/mockito-all/1.10.19/mockito-all-1.10.19.jar:/Users/xwx/.m2/repository/javax/xml/bind/jaxb-api/2.2.11/jaxb-api-2.2.11.jar cn.llzg.opinions.utils.Complecity
fac=1
cnt=1 i=1 count/i=1.0
fac=2
cnt=3 i=2 count/i=1.5
fac=6
cnt=5 i=3 count/i=1.6666666666666667
fac=24
cnt=7 i=4 count/i=1.75
fac=120
cnt=9 i=5 count/i=1.8
fac=720
cnt=11 i=6 count/i=1.8333333333333333
fac=5040
cnt=13 i=7 count/i=1.8571428571428572
fac=40320
cnt=15 i=8 count/i=1.875
fac=362880
cnt=17 i=9 count/i=1.8888888888888888
fac=3628800
cnt=19 i=10 count/i=1.9
fac=39916800
cnt=21 i=11 count/i=1.9090909090909092
fac=479001600
cnt=23 i=12 count/i=1.9166666666666667
fac=6227020800
cnt=25 i=13 count/i=1.9230769230769231
fac=87178291200
cnt=27 i=14 count/i=1.9285714285714286
fac=1307674368000
cnt=29 i=15 count/i=1.9333333333333333
fac=20922789888000
cnt=31 i=16 count/i=1.9375
fac=355687428096000
cnt=33 i=17 count/i=1.9411764705882353
fac=6402373705728000
cnt=35 i=18 count/i=1.9444444444444444
fac=121645100408832000
cnt=37 i=19 count/i=1.9473684210526316
fac=2432902008176640000
cnt=39 i=20 count/i=1.95
fac=51090942171709440000
cnt=41 i=21 count/i=1.9523809523809523
fac=1124000727777607680000
cnt=43 i=22 count/i=1.9545454545454546
fac=25852016738884976640000
cnt=45 i=23 count/i=1.9565217391304348
fac=620448401733239439360000
cnt=47 i=24 count/i=1.9583333333333333
fac=15511210043330985984000000
cnt=49 i=25 count/i=1.96
fac=403291461126605635584000000
cnt=51 i=26 count/i=1.9615384615384615
fac=10888869450418352160768000000
cnt=53 i=27 count/i=1.962962962962963
fac=304888344611713860501504000000
cnt=55 i=28 count/i=1.9642857142857142
fac=8841761993739701954543616000000
cnt=57 i=29 count/i=1.9655172413793103
fac=265252859812191058636308480000000
cnt=59 i=30 count/i=1.9666666666666666
fac=8222838654177922817725562880000000
cnt=61 i=31 count/i=1.967741935483871
fac=263130836933693530167218012160000000
cnt=63 i=32 count/i=1.96875
fac=8683317618811886495518194401280000000
cnt=65 i=33 count/i=1.9696969696969697
fac=295232799039604140847618609643520000000
cnt=67 i=34 count/i=1.9705882352941178
fac=10333147966386144929666651337523200000000
cnt=69 i=35 count/i=1.9714285714285715
fac=371993326789901217467999448150835200000000
cnt=71 i=36 count/i=1.9722222222222223
fac=13763753091226345046315979581580902400000000
cnt=73 i=37 count/i=1.972972972972973
fac=523022617466601111760007224100074291200000000
cnt=75 i=38 count/i=1.9736842105263157
fac=20397882081197443358640281739902897356800000000
cnt=77 i=39 count/i=1.9743589743589745
fac=815915283247897734345611269596115894272000000000
cnt=79 i=40 count/i=1.975
fac=33452526613163807108170062053440751665152000000000
cnt=81 i=41 count/i=1.975609756097561
fac=1405006117752879898543142606244511569936384000000000
cnt=83 i=42 count/i=1.9761904761904763
fac=60415263063373835637355132068513997507264512000000000
cnt=85 i=43 count/i=1.9767441860465116
fac=2658271574788448768043625811014615890319638528000000000
cnt=87 i=44 count/i=1.9772727272727273
fac=119622220865480194561963161495657715064383733760000000000
cnt=89 i=45 count/i=1.9777777777777779
fac=5502622159812088949850305428800254892961651752960000000000
cnt=91 i=46 count/i=1.9782608695652173
fac=258623241511168180642964355153611979969197632389120000000000
cnt=93 i=47 count/i=1.9787234042553192
fac=12413915592536072670862289047373375038521486354677760000000000
cnt=95 i=48 count/i=1.9791666666666667
fac=608281864034267560872252163321295376887552831379210240000000000
cnt=97 i=49 count/i=1.9795918367346939
fac=30414093201713378043612608166064768844377641568960512000000000000
cnt=99 i=50 count/i=1.98
fac=1551118753287382280224243016469303211063259720016986112000000000000
cnt=101 i=51 count/i=1.9803921568627452
fac=80658175170943878571660636856403766975289505440883277824000000000000
cnt=103 i=52 count/i=1.9807692307692308
fac=4274883284060025564298013753389399649690343788366813724672000000000000
cnt=105 i=53 count/i=1.9811320754716981
fac=230843697339241380472092742683027581083278564571807941132288000000000000
cnt=107 i=54 count/i=1.9814814814814814
fac=12696403353658275925965100847566516959580321051449436762275840000000000000
cnt=109 i=55 count/i=1.981818181818182
fac=710998587804863451854045647463724949736497978881168458687447040000000000000
cnt=111 i=56 count/i=1.9821428571428572
fac=40526919504877216755680601905432322134980384796226602145184481280000000000000
cnt=113 i=57 count/i=1.9824561403508771
fac=2350561331282878571829474910515074683828862318181142924420699914240000000000000
cnt=115 i=58 count/i=1.9827586206896552
fac=138683118545689835737939019720389406345902876772687432540821294940160000000000000
cnt=117 i=59 count/i=1.9830508474576272
fac=8320987112741390144276341183223364380754172606361245952449277696409600000000000000
cnt=119 i=60 count/i=1.9833333333333334
fac=507580213877224798800856812176625227226004528988036003099405939480985600000000000000
cnt=121 i=61 count/i=1.9836065573770492
fac=31469973260387937525653122354950764088012280797258232192163168247821107200000000000000
cnt=123 i=62 count/i=1.9838709677419355
fac=1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000
cnt=125 i=63 count/i=1.9841269841269842
fac=126886932185884164103433389335161480802865516174545192198801894375214704230400000000000000
cnt=127 i=64 count/i=1.984375
fac=8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
cnt=129 i=65 count/i=1.9846153846153847
fac=544344939077443064003729240247842752644293064388798874532860126869671081148416000000000000000
cnt=131 i=66 count/i=1.9848484848484849
fac=36471110918188685288249859096605464427167635314049524593701628500267962436943872000000000000000
cnt=133 i=67 count/i=1.9850746268656716
fac=2480035542436830599600990418569171581047399201355367672371710738018221445712183296000000000000000
cnt=135 i=68 count/i=1.9852941176470589
fac=171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
cnt=137 i=69 count/i=1.9855072463768115
fac=11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000
cnt=139 i=70 count/i=1.9857142857142858
fac=850478588567862317521167644239926010288584608120796235886430763388588680378079017697280000000000000000
cnt=141 i=71 count/i=1.9859154929577465
fac=61234458376886086861524070385274672740778091784697328983823014963978384987221689274204160000000000000000
cnt=143 i=72 count/i=1.9861111111111112
fac=4470115461512684340891257138125051110076800700282905015819080092370422104067183317016903680000000000000000
cnt=145 i=73 count/i=1.9863013698630136
fac=330788544151938641225953028221253782145683251820934971170611926835411235700971565459250872320000000000000000
cnt=147 i=74 count/i=1.9864864864864864
fac=24809140811395398091946477116594033660926243886570122837795894512655842677572867409443815424000000000000000000
cnt=149 i=75 count/i=1.9866666666666666
fac=1885494701666050254987932260861146558230394535379329335672487982961844043495537923117729972224000000000000000000
cnt=151 i=76 count/i=1.986842105263158
fac=145183092028285869634070784086308284983740379224208358846781574688061991349156420080065207861248000000000000000000
cnt=153 i=77 count/i=1.9870129870129871
fac=11324281178206297831457521158732046228731749579488251990048962825668835325234200766245086213177344000000000000000000
cnt=155 i=78 count/i=1.9871794871794872
fac=894618213078297528685144171539831652069808216779571907213868063227837990693501860533361810841010176000000000000000000
cnt=157 i=79 count/i=1.9873417721518987
fac=71569457046263802294811533723186532165584657342365752577109445058227039255480148842668944867280814080000000000000000000
cnt=159 i=80 count/i=1.9875
fac=5797126020747367985879734231578109105412357244731625958745865049716390179693892056256184534249745940480000000000000000000
cnt=161 i=81 count/i=1.9876543209876543
fac=475364333701284174842138206989404946643813294067993328617160934076743994734899148613007131808479167119360000000000000000000
cnt=163 i=82 count/i=1.9878048780487805
fac=39455239697206586511897471180120610571436503407643446275224357528369751562996629334879591940103770870906880000000000000000000
cnt=165 i=83 count/i=1.9879518072289157
fac=3314240134565353266999387579130131288000666286242049487118846032383059131291716864129885722968716753156177920000000000000000000
cnt=167 i=84 count/i=1.9880952380952381
fac=281710411438055027694947944226061159480056634330574206405101912752560026159795933451040286452340924018275123200000000000000000000
cnt=169 i=85 count/i=1.988235294117647
fac=24227095383672732381765523203441259715284870552429381750838764496720162249742450276789464634901319465571660595200000000000000000000
cnt=171 i=86 count/i=1.9883720930232558
fac=2107757298379527717213600518699389595229783738061356212322972511214654115727593174080683423236414793504734471782400000000000000000000
cnt=173 i=87 count/i=1.9885057471264367
fac=185482642257398439114796845645546284380220968949399346684421580986889562184028199319100141244804501828416633516851200000000000000000000
cnt=175 i=88 count/i=1.9886363636363635
fac=16507955160908461081216919262453619309839666236496541854913520707833171034378509739399912570787600662729080382999756800000000000000000000
cnt=177 i=89 count/i=1.9887640449438202
fac=1485715964481761497309522733620825737885569961284688766942216863704985393094065876545992131370884059645617234469978112000000000000000000000
cnt=179 i=90 count/i=1.988888888888889
fac=135200152767840296255166568759495142147586866476906677791741734597153670771559994765685283954750449427751168336768008192000000000000000000000
cnt=181 i=91 count/i=1.989010989010989
fac=12438414054641307255475324325873553077577991715875414356840239582938137710983519518443046123837041347353107486982656753664000000000000000000000
cnt=183 i=92 count/i=1.9891304347826086
fac=1156772507081641574759205162306240436214753229576413535186142281213246807121467315215203289516844845303838996289387078090752000000000000000000000
cnt=185 i=93 count/i=1.989247311827957
fac=108736615665674308027365285256786601004186803580182872307497374434045199869417927630229109214583415458560865651202385340530688000000000000000000000
cnt=187 i=94 count/i=1.9893617021276595
fac=10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000
cnt=189 i=95 count/i=1.9894736842105263
fac=991677934870949689209571401541893801158183648651267795444376054838492222809091499987689476037000748982075094738965754305639874560000000000000000000000
cnt=191 i=96 count/i=1.9895833333333333
fac=96192759682482119853328425949563698712343813919172976158104477319333745612481875498805879175589072651261284189679678167647067832320000000000000000000000
cnt=193 i=97 count/i=1.9896907216494846
fac=9426890448883247745626185743057242473809693764078951663494238777294707070023223798882976159207729119823605850588608460429412647567360000000000000000000000
cnt=195 i=98 count/i=1.989795918367347
fac=933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
cnt=197 i=99 count/i=1.9898989898989898

可见 count/i 几乎是常数,所以时间复杂度应是: O(n)
空间复杂度可以通过分配内存测量, 因为是用的二分法, n每增加一, 栈多一层,应该是O(log n ).

根据 @fefe 的补充,和这里的讨论 https://stackoverflow.com/que... 可以看出大数乘法多采用适应性的混合算法,就是算小数和大数时用的算法不太一样,但基本是小于O(n^2)的, 好一些的在O(n^1.5)左右。取大原则, 最终在算法时间复杂度也是由大数乘法决定的。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文