#!/usr/bin/env php <?php class Complex { public $real = 0; private $imag = 0; public function __construct($real, $imag = null) { $this->real = (float)$real; } public function __toString() { return sprintf("(%d+%dj)", $this->real, $this->imag); } } $start = microtime(TRUE); $primeNumbers = array(); $output = ''; for ($i = 2; $i < 100000; $i++) { $divisible = false; $in = new Complex($i); foreach($primeNumbers as $number) { if ($in->real % $number->real == 0) { $divisible = true; break; } } if ($divisible == false){ $primeNumbers[] = $in; $output .= $in; } } echo "time: ", microtime(TRUE) - $start, "\n"; echo count($primeNumbers), " ", strlen($output), "\n"; ?>
#!/usr/bin/env python import time class Complex(object): def __init__(self, real, imag=None): self.real = real self.imag = 0 def __str__(self): return "({0}+{1}j)".format(self.real, self.imag) start = time.time() primeNumbers = [] output = "" for i in xrange(2, 100000): divisible = False inum = Complex(i) for number in primeNumbers: if inum.real % number.real == 0: divisible = True break if divisible == False: primeNumbers.append(inum) output += str(inum) print 'time: %f' % (time.time() - start) print len(primeNumbers), len(output)
Source: https://habr.com/ru/post/124346/
All Articles