Algorithm exercise-integer splitting into prime product

Source: Internet
Author: User

Problem:

Input 6

Output 1*2*3


1. List of build primes

2. From the Prime table in order to take prime numbers, N in addition to prime numbers, if not divisible, the Prime table index + +, otherwise, n/= Prime, continue to judge

3. Ergodic Prime List

4. Limited: The prime table needs to be large enough, if the number is very large, this algorithm needs to change


JS implementation:


function P (n) {if (n<2) {return 0;} if (n = = 2) {return 1;} for (var i = 2; i < n; i++) {if (n%i = = 0) {return 0;}} return 1;} var pl = new Array (), for (var i = 2,index = 0;index<100;i++) {if (P (i) = = 1) {Pl.push (i); index++;}} function f (n) {var pindex = 0;var ret = "1"; for (;p index<100;) {if (n% pl[pindex] = = 0) {ret + = "*" + pl[pindex]; n/=pl[pindex];} Else{if (P (n)) {ret + = "*" + n;return ret;} Pindex + +;}} return ret;} Console.log (f (111223));




Algorithm exercise-integer splitting into prime product

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.