[算法]: 质因数分解

任意一个大于等于2的正整数都可以质因数分解

C++

#include <iostream>
#include <cmath>

using namespace std;

#define maxn 1000000

void factor(int n, int a[maxn], int b[maxn], int& tot) {
    
    
    int temp, i, now;
    temp = (int)(sqrt((double)n) + 1);
    tot = 0;
    now = n;
    for (i = 2; i <= temp; i++) if (now % i == 0) {
    
    
        a[++tot] = i;
        b[tot] = 0;
        while(now % i == 0) {
    
    
            ++b[tot];
            now /= i;
        }
    }
    if (now != 1) {
    
    
        a[++tot] = now;
        b[tot] = 1;
    }
}

int main() {
    
    
    int n;
    while (true) {
    
    
        cin >> n;
        int a[maxn], b[maxn];
        int tot = 0;
        factor(n, a, b, tot);
        cout << n << " = ";
        for (int i = 1; i <= tot; i++) {
    
    
            for (int j = 1; j <= b[i]; j++) {
    
    
                cout << a[i];
                if (i == tot && j == b[i]) cout << endl;
                else cout << " * ";
            }
        }
    }
    return 0;
}

JAVA

package demo;

import java.util.Scanner;

public class Factor {
    
    
    private static int tot = 0;
    private static void factor(int n, int[] a, int[] b) {
    
    
        tot = 0;
        int i, now;
        int temp = (int)(Math.sqrt((double)(n))) + 1;
        now = n;
        for (i = 2; i <= temp; i++) if (now % i == 0) {
    
    
            a[++tot] = i;
            b[tot] = 0;
            while (now % i == 0) {
    
    
                ++b[tot];
                now /= i;
            }
        }
        if (now != 1) {
    
    
            a[++tot] = now;
            b[tot] = 1;
        }
    }

    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        int n;
        while (true) {
    
    
            n = input.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            factor(n, a, b);
            System.out.print(n + " = ");
            for (int i = 1; i <= tot; i++) {
    
    
                for (int j = 1; j <= b[i]; j++) {
    
    
                    System.out.print(a[i]);
                    if (i == tot && j == b[i]) System.out.println();
                    else System.out.print(" * ");
                }
            }
        }
    }
}

Python

import numpy as np


def factor(n, a, b, tot_):
    tot = 0
    temp = int(np.sqrt(n) + 1)
    now = n
    for i in range(2, temp + 1):
        if now % i == 0:
            tot += 1
            a[tot] = i
            while now % i == 0:
                b[tot] += 1
                now /= i
    if now != 1:
        tot += 1
        a[tot] = int(now)
        b[tot] = 1
    tot_[0] = tot


if __name__ == '__main__':
    while True:
        n = int(input())
        a = [0 for i in range(n + 1)]
        b = [0 for i in range(n + 1)]
        tot = [0]
        factor(n, a, b, tot)
        num = tot[0]
        print(num, "= ", end='')
        for i in range(1, num + 1):
            for j in range(0, b[i]):
                print(a[i], end='')
                if i == num and j == b[i] - 1:
                    print()
                else:
                    print(" * ", end='')

猜你喜欢

转载自blog.csdn.net/qq_27198345/article/details/109703157