JSは正の整数分解素数を実現します.例えば、90を入力して、90=2*3*3*5を印刷します.

4092 ワード




    
    Title



    <span style="color: #008000;">//</span><span style="color: #008000;">            。  :  90,   90=2*3*3*5</span>
    <span style="color: #008000;">//</span><span style="color: #008000;">       90</span>
    <span style="color: #0000ff;">var</span> a =<span style="color: #000000;"> Number(prompt());
    </span><span style="color: #0000ff;">var</span> c = ""<span style="color: #000000;">;
    </span><span style="color: #0000ff;">var</span> d =<span style="color: #000000;"> a;
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> b = 2; b <= a; b++<span style="color: #000000;">) {
        </span><span style="color: #008000;">//</span><span style="color: #008000;">   a%b === 0;</span>
        <span style="color: #0000ff;">if</span> (a % b === 0 && a !==<span style="color: #000000;"> b) {
            </span><span style="color: #008000;">/*</span><span style="color: #008000;">
              a%b       c   90/2=45 45/3=15 15/3=5  5/5=1  c    2 3 3
            a!==b   5/5  a=5 b=5;;       5
                a !== b
                    
            90 = 2*3*3*5*
            5    *     
            5      
            </span><span style="color: #008000;">*/</span><span style="color: #000000;">
            c </span>+= b + "*"<span style="color: #000000;">;
            </span><span style="color: #008000;">/*</span><span style="color: #008000;">
              a /= b;        a= a / b
            90/2=45              ? = 90 / 2
            45/3=15              ? = 90 / 2 / 3
            15/3=5               ? = 90 / 2 / 3 / 3
            5/5=1                ? = 90 / 2 / 3 / 3 / 5
            </span><span style="color: #008000;">*/</span><span style="color: #000000;">
            a </span>/= b;
            <span style="color: #008000;">/*</span><span style="color: #008000;">
               b = 2
            b   1   2 3 4 5 6 7 8 9   <=a;
              90 = 2 * 3 * 3 * 5          3       
                    b = 2;
            90 / 2 = 45(    )
                    b = 3
            45 / (2 - 1 = 1) (    )
            45 / (1 + 1 = 2 ) (    )
            45 / (2 + 1 = 3) = 15 (    )
                    b = 3
            15 / (3 - 1 = 2) (    )
            15 / (2 + 1 = 3) = 5 (    )
                
                   
            a !== b
                  5 / 5
                    a !== b   
             </span><span style="color: #008000;">*/</span><span style="color: #000000;">
             b</span>--<span style="color: #000000;">;
            </span><span style="color: #008000;">/*</span><span style="color: #008000;">
                    a === b   5 / 5
                a                        15 / (2 + 1 = 3) = 5
             </span><span style="color: #008000;">*/</span><span style="color: #000000;">
        }</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (a ===<span style="color: #000000;"> b){
            </span><span style="color: #008000;">//</span><span style="color: #008000;">   c     a / b    a,b  (2 3 3 5)</span>
            c +=<span style="color: #000000;"> b;
            </span><span style="color: #008000;">//</span><span style="color: #008000;">   a === b     5 / 5 = 1  1       </span>
            <span style="color: #0000ff;">break</span><span style="color: #000000;">;
        }
    }
    console.log(d</span>+"="+<span style="color: #000000;">c);
</span>

90=2*3*3*5