Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
66
Task/Additive-primes/Fortran/additive-primes.f
Normal file
66
Task/Additive-primes/Fortran/additive-primes.f
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
program AdditivePrimes
|
||||
implicit none
|
||||
|
||||
integer :: i, j, digit_sum, count
|
||||
logical :: is_prime
|
||||
|
||||
! Arrays to track prime numbers and additive primes
|
||||
logical, dimension(500) :: prime_check
|
||||
logical, dimension(500) :: additive_prime_check
|
||||
|
||||
! Initialize arrays
|
||||
prime_check = .true.
|
||||
prime_check(1) = .false.
|
||||
additive_prime_check = .false.
|
||||
|
||||
! Sieve of Eratosthenes to find primes
|
||||
do i = 2, int(sqrt(real(500)))
|
||||
if (prime_check(i)) then
|
||||
do j = i*i, 500, i
|
||||
prime_check(j) = .false.
|
||||
end do
|
||||
end if
|
||||
end do
|
||||
|
||||
! Find additive primes
|
||||
count = 0
|
||||
do i = 2, 500
|
||||
if (prime_check(i)) then
|
||||
! Calculate digit sum
|
||||
digit_sum = sum_digits(i)
|
||||
|
||||
! Check if digit sum is also prime
|
||||
if (prime_check(digit_sum)) then
|
||||
additive_prime_check(i) = .true.
|
||||
count = count + 1
|
||||
end if
|
||||
end if
|
||||
end do
|
||||
|
||||
! Print results
|
||||
print *, "Additive Primes less than 500:"
|
||||
do i = 2, 500
|
||||
if (additive_prime_check(i)) then
|
||||
print *, i
|
||||
end if
|
||||
end do
|
||||
|
||||
print *, "Total number of additive primes:", count
|
||||
|
||||
contains
|
||||
|
||||
! Function to calculate sum of digits
|
||||
function sum_digits(num) result(total)
|
||||
integer, intent(in) :: num
|
||||
integer :: total, temp_num
|
||||
|
||||
total = 0
|
||||
temp_num = num
|
||||
|
||||
do while (temp_num > 0)
|
||||
total = total + mod(temp_num, 10)
|
||||
temp_num = temp_num / 10
|
||||
end do
|
||||
end function sum_digits
|
||||
|
||||
end program AdditivePrimes
|
||||
|
|
@ -1,15 +1,15 @@
|
|||
val isPrime = fn(i) {
|
||||
i == 2 or i > 2 and
|
||||
not any(fn x: i div x, pseries(2 .. i ^/ 2))
|
||||
not any(series(2 .. i ^/ 2, asconly=true), by=fn x:i div x)
|
||||
}
|
||||
|
||||
val sumDigits = fn i: fold(fn{+}, s2n(string(i)))
|
||||
val sumDigits = fn i: fold(s2n(string(i)), by=fn{+})
|
||||
|
||||
writeln "Additive primes less than 500:"
|
||||
|
||||
var cnt = 0
|
||||
|
||||
for i in [2] ~ series(3..500, 2) {
|
||||
for i in [2] ~ series(3..500, inc=2) {
|
||||
if isPrime(i) and isPrime(sumDigits(i)) {
|
||||
write "{{i:3}} "
|
||||
cnt += 1
|
||||
|
|
|
|||
26
Task/Additive-primes/Scala/additive-primes.scala
Normal file
26
Task/Additive-primes/Scala/additive-primes.scala
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
def isPrime(n: Int): Boolean = {
|
||||
@annotation.tailrec
|
||||
def checkDivisor(d: Int): Boolean = {
|
||||
if (d * d > n) true
|
||||
else if (n % d == 0) false
|
||||
else checkDivisor(d + 2)
|
||||
}
|
||||
|
||||
if (n < 2) false
|
||||
else if (n == 2 || n == 3) true
|
||||
else if (n % 2 == 0 || n % 3 == 0) false
|
||||
else checkDivisor(5)
|
||||
}
|
||||
|
||||
private def digitSum(n: Int): Int = n.toString.map(_ - '0').sum
|
||||
|
||||
private def additivePrime(n: Int): Boolean = isPrime(n) && isPrime(digitSum(n))
|
||||
|
||||
private def testAdditivePrime(max: Int): Unit = {
|
||||
val result = (2 to max).filter(additivePrime)
|
||||
println(result.mkString(", "))
|
||||
println(s"Found ${result.length} additive primes less than 500.")
|
||||
}
|
||||
|
||||
@main def main(): Unit =
|
||||
testAdditivePrime(500)
|
||||
Loading…
Add table
Add a link
Reference in a new issue