0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pascal, Go,Rustによるfactor

Last updated at Posted at 2024-11-18

PascalによるPrimeFactorizationです。AI Code Converterで変換しました。
Pascalは可変長配列(vector)が使えないので、Cで書いたソースをAI Code Converterに掛けました。

PASCAL Compilerのインストール to Archlinux: sudo pacman -S fpc
compile: fpc factor.pas
使い方:factor <n>

factor.pas
program PrimeFactorization;

uses sysutils;

function PrimeFactorization(num: Integer; var arr: array of Integer): Integer;
var
  i, j: Integer;
begin
  i := 2;
  j := 0;

  if num = 0 then
    exit(0)
  else if num = 1 then
    exit(0);

  while num <> 1 do
  begin
    if num mod i = 0 then
    begin
      arr[j] := i;
      num := num div i;
      j := j + 1;
    end
    else
      i := i + 1;
  end;

  PrimeFactorization := j;
end;

var
  arr: array[0..9999] of Integer;
  n, nop, i: Integer;

begin
  n := StrToInt(ParamStr(1));
  nop := PrimeFactorization(n, arr);

  Write(n, ' : ');
  for i := 0 to nop - 1 do
    Write(arr[i], ' ');
  Writeln;
end.

go

こちらはgoによる、factorです。goではsliceが使えるので、pythonで書いたコードをAI Code Converterに掛けました。

factor.go
package main

import (
    "fmt"
    "os"
    "strconv"
)

func PrimeFactorization(n int) []int {
    i := 2
    j := 0
    var f []int

    if n == 0 {
        return []int{}
    } else if n == 1 {
        return []int{1}
    }

    for n != 1 {
        if n%i == 0 {
            f = append(f, i)
            n = n / i
            j = j + 1
        } else {
            i = i + 1
        }
    }

    return f
}

func main() {
    args := os.Args[1:]
    if len(args) < 1 {
        fmt.Println("Please provide an integer argument")
        os.Exit(1)
    }

    n, err := strconv.Atoi(args[0])
    if err != nil {
        fmt.Println("Invalid input. Please provide a valid integer")
        os.Exit(1)
    }

    f := PrimeFactorization(n)
    fmt.Println(n, ":", f)
}

golang install to arch linux: sudo pacman -S go
compile: go build factor.go
execution: ./factor N

Rust

こちらはRustによるPrimeFactorizationです。可変長配列にvectorを使ってます。

factor.ru
fn prime_factorization(n: i32) -> Vec<i32> {
    let mut i = 2;
    let mut f: Vec<i32> = Vec::new();

    if n == 0 {
        return Vec::new();
    } else if n == 1 {
        return vec![1];
    }

    let mut num = n;
    while num != 1 {
        if num % i == 0 {
            f.push(i);
            num /= i;
        } else {
            i += 1;
        }
    }

    f
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() < 2 {
        println!("Please provide an integer argument");
        std::process::exit(1);
    }

    let n: i32 = match args[1].parse() {
        Ok(num) => num,
        Err(_) => {
            println!("Invalid input. Please provide a valid integer");
            std::process::exit(1);
        }
    };

    let f = prime_factorization(n);
    println!("{}: {:?}", n, f);
}

compile: rustc factor.ru
execution: ./factor N

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?