June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
4
Task/Semiprime/Factor/semiprime-1.factor
Normal file
4
Task/Semiprime/Factor/semiprime-1.factor
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
USING: kernel math.combinatorics math.primes.factors sequences ;
|
||||
: semiprime? ( n -- ? )
|
||||
[ factors 2 <combinations> [ product ] map ]
|
||||
[ [ = ] curry ] bi any? ;
|
||||
1
Task/Semiprime/Factor/semiprime-2.factor
Normal file
1
Task/Semiprime/Factor/semiprime-2.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
100 iota [ semiprime? ] filter [ pprint bl ] each nl
|
||||
|
|
@ -1 +1,3 @@
|
|||
semiprime(n) = sum(values(factor(n))) == 2
|
||||
using Primes
|
||||
issemiprime(n::Integer) = sum(values(factor(n))) == 2
|
||||
@show filter(issemiprime, 1:100)
|
||||
|
|
|
|||
13
Task/Semiprime/Lingo/semiprime-1.lingo
Normal file
13
Task/Semiprime/Lingo/semiprime-1.lingo
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
on isSemiPrime (n)
|
||||
div = 2
|
||||
cnt = 0
|
||||
repeat while cnt < 3 and n <> 1
|
||||
if n mod div = 0 then
|
||||
n = n / div
|
||||
cnt = cnt + 1
|
||||
else
|
||||
div = div + 1
|
||||
end if
|
||||
end repeat
|
||||
return cnt=2
|
||||
end
|
||||
5
Task/Semiprime/Lingo/semiprime-2.lingo
Normal file
5
Task/Semiprime/Lingo/semiprime-2.lingo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
res = []
|
||||
repeat with i = 1 to 100
|
||||
if isSemiPrime(i) then res.add(i)
|
||||
end repeat
|
||||
put res
|
||||
|
|
@ -1,12 +1,11 @@
|
|||
sub is-semiprime (Int $n --> Bool) {
|
||||
not $n.is-prime and
|
||||
.is-prime given
|
||||
$n div first $n %% *,
|
||||
grep &is-prime, 2 .. *;
|
||||
$n div first $n %% *, flat grep &is-prime, 2 .. *;
|
||||
}
|
||||
|
||||
use Test;
|
||||
my @primes = grep &is-prime, 2 .. 100;
|
||||
my @primes = flat grep &is-prime, 2 .. 100;
|
||||
for ^5 {
|
||||
nok is-semiprime([*] my @f1 = @primes.roll(1)), ~@f1;
|
||||
ok is-semiprime([*] my @f2 = @primes.roll(2)), ~@f2;
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ sub find-factor ( Int $n, $constant = 1 ) {
|
|||
INIT my $start = now;
|
||||
|
||||
# Infinite list of semiprimes
|
||||
constant @semiprimes = 4, 6, 9, -> $p { ($p + 1 ... &is-semiprime).tail } ... *;
|
||||
constant @semiprimes = lazy gather for 4 .. * { .take if .&is-semiprime };
|
||||
|
||||
# Show the semiprimes < 100
|
||||
say 'Semiprimes less than 100:';
|
||||
|
|
@ -33,6 +33,6 @@ say @semiprimes[^ @semiprimes.first: * > 100, :k ], "\n";
|
|||
|
||||
# Check individual integers, or in this case, a range
|
||||
my $s = 2⁹⁷ - 1;
|
||||
say "Is $_ semiprime?: ", is-semiprime( $_ ) for $s .. $s + 30;
|
||||
say "Is $_ semiprime?: ", .&is-semiprime for $s .. $s + 30;
|
||||
|
||||
say 'elapsed seconds: ', now - $start;
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ next
|
|||
func isPrime n
|
||||
if n < 2 return false ok
|
||||
if n < 4 return true ok
|
||||
if n % 2 = 0 return false ok
|
||||
if n % 2 = 0 and n != 2 return false ok
|
||||
for d = 3 to sqrt(n) step 2
|
||||
if n % d = 0 return false ok
|
||||
next
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ require 'prime'
|
|||
|
||||
class Integer
|
||||
def semi_prime?
|
||||
prime_division.map( &:last ).inject( &:+ ) == 2
|
||||
prime_division.map( &:last ).sum == 2
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
56
Task/Semiprime/Rust/semiprime.rust
Normal file
56
Task/Semiprime/Rust/semiprime.rust
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
extern crate primal;
|
||||
|
||||
fn isqrt(n: usize) -> usize {
|
||||
(n as f64).sqrt() as usize
|
||||
}
|
||||
|
||||
fn is_semiprime(mut n: usize) -> bool {
|
||||
let root = isqrt(n) + 1;
|
||||
let primes1 = primal::Sieve::new(root);
|
||||
let mut count = 0;
|
||||
|
||||
for i in primes1.primes_from(2).take_while(|&x| x < root) {
|
||||
while n % i == 0 {
|
||||
n /= i;
|
||||
count += 1;
|
||||
}
|
||||
if n == 1 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if n != 1 {
|
||||
count += 1;
|
||||
}
|
||||
count == 2
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
assert_eq!((2..10).filter(|&n| is_semiprime(n)).count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test2() {
|
||||
assert_eq!((2..100).filter(|&n| is_semiprime(n)).count(), 34);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test3() {
|
||||
assert_eq!((2..1_000).filter(|&n| is_semiprime(n)).count(), 299);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test4() {
|
||||
assert_eq!((2..10_000).filter(|&n| is_semiprime(n)).count(), 2_625);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test5() {
|
||||
assert_eq!((2..100_000).filter(|&n| is_semiprime(n)).count(), 23_378);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test6() {
|
||||
assert_eq!((2..1_000_000).filter(|&n| is_semiprime(n)).count(), 210_035);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue