Home > php教程 > PHP源码 > 一个双射类,在给定自然数空间一对一完全映射,可逆运算,不可猜解

一个双射类,在给定自然数空间一对一完全映射,可逆运算,不可猜解

PHP中文网
Release: 2016-05-25 16:59:18
Original
1225 people have browsed it

/**
 * 双射类
 * 在给定自然数空间一对一完全映射,可逆运算,不可猜解
 * @author liuxu
 *
 */
class Bijective
{
	static private $defaultPrime = '85310501873';//请使用个位为"3"、"7"、"9"的质数

	static public function getRandPrime()
	{
		return self::getMinPrime(rand(100000,900000)*100000+rand(100000,900000));
	}

	static public function getMinPrime($min)
	{
		$number = $min;
		while(true)
		{
			if(self::isPrime($number)) break;
			$number++;
		}

		return $number;
	}

	static public function isPrime($number)
	{
		$sqrt = intval(bcsqrt($number,0));
		for($i=2;$i<=$sqrt;$i++)
		{
			if(bcmod($number,$i)===&#39;0&#39;)
			{
				return false;
			}
		}

		return true;
	}

	static public function mod($number,$len=10)
	{
		return bcmod(bcmul($number,self::$defaultPrime,0),bcpow(10,$len,0));
	}

	static public function loop($number,$len=8)
	{

		$min = $prev = $number;
		for($i=1;;$i++)
		{
			$next = self::mod($prev,$len);
			if($next==$number) break;
			$min = min($min,$next);
			$prev = $next;
		}

		$offset = ( $min % $i ) + 1;
		for($j=0;$j<$offset;$j++)
		{
			$number = self::mod($number,$len);
		}

		return $number;
	}

	static public function insanity($number,$len=10)
	{
		$number = self::mod($number,$len);
		$number = str_pad($number,$len,&#39;0&#39;,STR_PAD_LEFT);

		$numberList = str_split($number,6);
		foreach($numberList as $key=>$value)
		{
			$numberList[$key] = str_pad(self::loop($value,strlen($value)),strlen($value),&#39;0&#39;,STR_PAD_LEFT);
		}

		$number = implode(&#39;&#39;,$numberList);

		return $number;
	}

}
Copy after login

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template