Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
27
Task/Factorial-primes/Common-Lisp/factorial-primes.lisp
Normal file
27
Task/Factorial-primes/Common-Lisp/factorial-primes.lisp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(defun factorial (x)
|
||||
(if (= x 1)
|
||||
x
|
||||
(* x (factorial (- x 1)))))
|
||||
|
||||
(defun is-factor (x y)
|
||||
(zerop (mod x y)))
|
||||
|
||||
(defun is-prime (n)
|
||||
(cond ((< n 4) (or (= n 2) (= n 3)))
|
||||
((or (zerop (mod n 2)) (zerop (mod n 3))) nil)
|
||||
(t (loop for i from 5 to (floor (sqrt n)) by 6
|
||||
never (or (is-factor n i)
|
||||
(is-factor n (+ i 2)))))))
|
||||
|
||||
(defun main (&optional (limit 10))
|
||||
(let ((n 0)
|
||||
(f 0))
|
||||
(loop while (< n limit)
|
||||
for i from 1
|
||||
do (setf f (factorial i))
|
||||
(when (is-prime (+ f 1))
|
||||
(incf n)
|
||||
(format t "~2d: ~2d! + 1 = ~12d~%" n i (+ f 1)))
|
||||
(when (is-prime (- f 1))
|
||||
(incf n)
|
||||
(format t "~2d: ~2d! - 1 = ~12d~%" n i (- f 1))))))
|
||||
59
Task/Factorial-primes/Go/factorial-primes.go
Normal file
59
Task/Factorial-primes/Go/factorial-primes.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func main() {
|
||||
n, count := 0, 0
|
||||
|
||||
for count < 10 {
|
||||
n++
|
||||
f := factorial(n)
|
||||
|
||||
if isPrime(f.Sub(f, big.NewInt(1))) {
|
||||
count++
|
||||
fmt.Printf("%2d: %2d! - 1 = %s\n", count, n, f.String())
|
||||
}
|
||||
|
||||
if isPrime(f.Add(f, big.NewInt(2))) {
|
||||
count++
|
||||
fmt.Printf("%2d: %2d! + 1 = %s\n", count, n, f.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func factorial(n int) *big.Int {
|
||||
result := big.NewInt(1)
|
||||
|
||||
for i := 2; i <= n; i++ {
|
||||
result.Mul(result, big.NewInt(int64(i)))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func isPrime(num *big.Int) bool {
|
||||
if num.Cmp(big.NewInt(2)) < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
if num.Cmp(big.NewInt(2)) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
if new(big.Int).Mod(num, big.NewInt(2)).Cmp(big.NewInt(0)) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
sqrt := new(big.Int).Sqrt(num)
|
||||
|
||||
for i := big.NewInt(3); i.Cmp(sqrt) <= 0; i.Add(i, big.NewInt(2)) {
|
||||
if new(big.Int).Mod(num, i).Cmp(big.NewInt(0)) == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
43
Task/Factorial-primes/Kotlin/factorial-primes.kotlin
Normal file
43
Task/Factorial-primes/Kotlin/factorial-primes.kotlin
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import java.math.BigInteger
|
||||
import java.math.BigInteger.ONE
|
||||
|
||||
enum class Difference(private val displayText: String) {
|
||||
MINUS_ONE("- 1"), PLUS_ONE("+ 1");
|
||||
|
||||
override fun toString(): String {
|
||||
return displayText
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
var currentFactorial = ONE
|
||||
var highestFactor = 1L
|
||||
var found = 0
|
||||
|
||||
while(found < 30) {
|
||||
if ((currentFactorial - ONE).isProbablePrime(25)) {
|
||||
printlnFactorialPrime(currentFactorial - ONE, highestFactor, Difference.MINUS_ONE)
|
||||
found++
|
||||
}
|
||||
if ((currentFactorial + ONE).isProbablePrime(25)) {
|
||||
printlnFactorialPrime(currentFactorial + ONE, highestFactor, Difference.PLUS_ONE)
|
||||
found++
|
||||
}
|
||||
|
||||
highestFactor++
|
||||
currentFactorial *= BigInteger.valueOf(highestFactor)
|
||||
}
|
||||
}
|
||||
|
||||
fun printlnFactorialPrime(factorialPrime: BigInteger, base: Long, difference: Difference) =
|
||||
println("${base}! $difference = ${factorialPrime.shortenIfNecessary()}")
|
||||
|
||||
fun BigInteger.shortenIfNecessary(): String {
|
||||
val digits = toString()
|
||||
val length = digits.length
|
||||
return if (length <= 40) {
|
||||
digits
|
||||
} else {
|
||||
"${digits.take(20)}...${digits.takeLast(20)} ($length digits)"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue