search
Homephp教程PHP源码大整数相乘法

大整数相乘法

May 25, 2016 pm 05:09 PM

跳至

<?php

/**
 * 大整数想乘
 */
//数字1
$n1 = "8274918237591826391827591827391827";
//数字2
$n2 = "0000000000129837293586928391837492837592837491873498216359187234986239487";

//九九乘法表
$muti = array();
for ($i = 0; $i < 10; $i++) {
    for ($j = 0; $j < 10; $j++) {
        $muti[strval($i)][strval($j)] = $i * $j;
    }
}

//最长长度
$len_1 = strlen($n1);
//最短长度
$len_2 = strlen($n2);

//结果长度
$len_r = $len_1+$len_2+1;
//运算结果
$result = array_fill(0, $len_r, 0);

//数字反序
$n1 = strrev($n1);
$n2 = strrev($n2);

//按位运算
for ($i = 0; $i < $len_1; $i++) {
    for ($j = 0; $j < $len_2; $j++) {
        $result[$i + $j] += $muti[$n1[$i]][$n2[$j]];
    }
}

//进位处理
$i = 0;
$j = $len_r-1;
do{
    $result[$i + 1] += (int) ($result[$i] / 10);
    $result[$i] = $result[$i] % 10;
}  while (++$i

                   

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools