Strong and weak primes: Difference between revisions

no edit summary
No edit summary
Line 1,093:
Number of weak primes below 1,000,000 = 37,780
Number of weak primes below 10,000,000 = 321,750</pre>
 
=={{header|Ksh}}==
<lang ksh>
#!/bin/ksh
 
# Strong and weak primes
# # Find and display (on one line) the first   36 strong primes.
# # Find and display the count of the strong primes below 1,000,000.
# # Find and display the count of the strong primes below 10,000,000.
# # Find and display (on one line) the first   37 weak primes.
# # Find and display the count of the weak primes below 1,000,000.
# # Find and display the count of the weak primes below 10,000,000.
# # (Optional) display the counts and "below numbers" with commas. ???
 
# # A strong prime is when prime[p] > (prime[p-1] + prime[p+1]) ÷ 2
# # A weak prime is when prime[p] < (prime[p-1] + prime[p+1]) ÷ 2
# # Balanced prime is when prime[p] = (prime[p-1] + prime[p+1]) ÷ 2
 
# # Variables:
#
integer NUM_STRONG=36 NUM_WEAK=37 GOAL1=1000000 MAX_INT=10000000
 
# # Functions:
#
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
 
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
 
# # Function _strength(prime[n], prime[n-1], prime[n+1]) return 1 for strong
#
function _strength {
typeset _pri ; integer _pri=$1 # PRIme number under consideration
typeset _pre ; integer _pre=$2 # PREvious prime number
typeset _nex ; integer _nex=$3 # NEXt prime number
typeset _result ; typeset -F1 _result
 
(( _result = (_pre + _nex) / 2.0 ))
(( _pri > _result )) && echo STRONG && return 0
(( _pri < _result )) && echo WEAK && return 1
echo BALANCED && return 99
}
 
#####
# main #
######
 
integer spcnt=0 wpcnt=0 bpcnt=0 sflg=0 wflg=0 i j k goal1_strong goal1_weak
typeset -C prime # prime[].val prime[].typ
typeset -a prime.val
typeset -a prime.typ
prime.typ[0]='NA' ; prime.typ[1]='NA'
 
for (( i=2; i<MAX_INT; i++ )); do
_isprime ${i} ; (( ! $? )) && continue
prime.val+=( ${i} )
(( ${#prime.val[*]} <= 2 )) && continue
 
(( j = ${#prime.val[*]} - 2 )) ; (( k = j - 1 ))
prime.typ+=( $(_strength ${prime.val[${j}]} ${prime.val[k]} ${prime.val[-1]}) )
case $? in
0) (( spcnt++ ))
(( spcnt <= NUM_STRONG )) && strbuff+="${prime.val[j]}, "
(( i >= GOAL1 )) && (( ! sflg )) && (( goal1_strong = spcnt - 1 )) && (( sflg = 1 ))
;;
 
1) (( wpcnt++ ))
(( wpcnt <= NUM_WEAK )) && weabuff+="${prime.val[j]}, "
(( i >= GOAL1 )) && (( ! wflg )) && (( goal1_weak = wpcnt - 1 )) && (( wflg = 1 ))
;;
 
99) (( bpcnt++ ))
;;
esac
done
 
printf "Total primes under %d = %d\n\n" $MAX_INT ${#prime.val[*]}
printf "First %d Strong Primes are: %s\n\n" $NUM_STRONG "${strbuff%,*}"
printf "Number of Strong Primes under %d is: %d\n" $GOAL1 ${goal1_strong}
printf "Number of Strong Primes under %d is: %d\n\n\n" $MAX_INT ${spcnt}
printf "First %d Weak Primes are: %s\n\n" $NUM_WEAK "${weabuff%,*}"
printf "Number of Weak Primes under %d is: %d\n" $GOAL1 ${goal1_weak}
printf "Number of Weak Primes under %d is: %d\n\n\n" $MAX_INT ${wpcnt}
printf "Number of Balanced Primes under %d is: %d\n\n\n" $MAX_INT ${bpcnt}</lang>
{{out}}<pre>
Total primes under 10000000 = 664579
 
First 36 Strong Primes are: 11, 17, 29, 37, 41, 59, 67, 71, 79, 97, 101, 107, 127, 137, 149, 163, 179, 191, 197, 223, 227, 239, 251, 269, 277, 281, 307, 311, 331, 347, 367, 379, 397, 419, 431, 439
 
Number of Strong Primes under 1000000 is: 37723
Number of Strong Primes under 10000000 is: 320991
 
First 37 Weak Primes are: 3, 7, 13, 19, 23, 31, 43, 47, 61, 73, 83, 89, 103, 109, 113, 131, 139, 151, 167, 181, 193, 199, 229, 233, 241, 271, 283, 293, 313, 317, 337, 349, 353, 359, 383, 389, 401
 
Number of Weak Primes under 1000000 is: 37779
Number of Weak Primes under 10000000 is: 321749
 
Number of Balanced Primes under 10000000 is: 21837</pre>
 
 
=={{header|Lua}}==
70

edits