Created
November 17, 2016 12:00
-
-
Save ldante86/f7f0f8e1e87c2b9681e060a35cc59d97 to your computer and use it in GitHub Desktop.
fibonacci and prime testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash - | |
. testnum-lib.sh | |
LIMIT=${1:-50000} | |
_is_valid_number $LIMIT || { | |
if [ $NAN ]; then | |
echo "$LIMIT is not a positive integer" | |
else | |
echo "$LIMIT is not big enough" | |
fi | |
exit 1 | |
} | |
_make_fibonacci_list | |
_find_fib_limit $LIMIT | |
_prime_fibonacci_number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash - | |
# Make a report on a given decimal. Test for prime numbers; | |
# if the number is not prime, factor it and print the list. | |
# Test for Fibonacci numbers. | |
if [ $# -eq 0 ]; then | |
echo "Usage: ${0##*/} number" | |
exit 1 | |
fi | |
. testnum-lib.sh | |
_is_valid_number "$1" | |
_is_prime_number "$1" | |
if [ $PRIME ]; then | |
echo Prime: Yes | |
elif [ $NAN ]; then | |
echo not a valid number | |
exit 1 | |
elif [ ! $UNFACTORABLE ]; then | |
echo Prime: No | |
echo Factored: "$FACT" | |
fi | |
_make_fibonacci_list | |
_is_fibonacci_number "$1" | |
if [ $FIB ]; then | |
echo Fibonacci: Yes | |
elif [ $NAN ]; then | |
echo not a valid number | |
else | |
echo Fibonacci: No | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash - | |
unset START STOP | |
while (( $# > 0 )) | |
do | |
case $1 in | |
-h*) | |
break | |
;; | |
-[0-9]*) | |
echo "not a positive integer $1" | |
break | |
;; | |
*[!0-9]*) | |
echo "invalid integer $1" | |
break | |
;; | |
*) | |
if [ $START ] | |
then | |
STOP=$1 | |
else | |
START=$1 | |
fi | |
if [ $START ] && [ $STOP ] | |
then | |
break | |
fi | |
;; | |
esac | |
shift | |
done | |
if (( STOP <= START )) | |
then | |
echo "usage: ${0##*/} start stop" | |
exit 1 | |
fi | |
for (( i=START; i<=STOP; i++ )) | |
do | |
num=$i | |
factor=0 | |
for (( t=2; t<=num; t++ )) | |
do | |
while (( num % t == O )) | |
do | |
(( factor++ )) | |
if (( factor > 1 )) | |
then | |
break 2 | |
else | |
(( num /= t )) | |
fi | |
done | |
done | |
if (( factor == 1 )) | |
then | |
echo $i | |
fi | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
_is_valid_number() | |
{ # Test if a number is valid. Return failure | |
# if not. | |
unset UNFACTORABLE # Zero and one cannot be factored. | |
unset NAN # At least one character is not 0-9. | |
case $1 in | |
0|1) | |
UNFACTORABLE=1 | |
return 1 | |
;; | |
*[!0-9]*|"") | |
NAN=1 | |
return 1 | |
;; | |
esac | |
} | |
_find_fib_limit() | |
{ # Given a number, count the number of Fibonacci | |
# numbers closest to the number. This value is | |
# loaded into the FIBLIMIT variable. | |
local i c | |
unset FIBLIMIT | |
for i in $FIBLIST | |
do | |
# If the number equals a number in FIBLIST, | |
# we use the index for that number. | |
if [ $i -eq $1 ]; then | |
FIBLIMIT=$c | |
break | |
# If the number is not equal but greater than | |
# a number in the list, use the index number | |
# of the previous number in the list. This is so | |
# we don't use more numbers than we need. | |
elif [ $i -gt $1 ]; then | |
FIBLIMIT=$((c-1)) | |
break | |
fi | |
# Index counter. Holds the amount of Fibonacci | |
# numbers to process in FIBLIST. | |
((c++)) | |
done | |
} | |
_is_fibonacci_number() | |
{ # Test if a number is a Fibonacci number. If true, | |
# set FIB to 1. | |
unset FIB | |
for f in $FIBLIST | |
do | |
if [ $f -eq $1 ]; then | |
FIB=1 | |
break | |
fi | |
done | |
} | |
_make_fibonacci_list() | |
{ # Load FIBLIST with a string of Fibonacci numbers | |
# delimited by a space. The bash interpreter can only | |
# generate up to the 92nd number in the sequence on a 64-bit | |
# system because after that large numbers are truncated. | |
unset FIBLIST | |
local a=0 b=1 sum i | |
for ((i=0; i<93; i++)) | |
do | |
FIBLIST="${FIBLIST} ${a}" | |
(( sum = a + b )) | |
a=$b | |
b=$sum | |
done | |
} | |
_prime_fibonacci_number() | |
{ # Given a set of Fibonacci numbers, print the | |
# numbers that are prime. | |
local g # Stores the Fibonacci number per loop. | |
local c=0 # Count up to and stop at FIBLIMIT. | |
local f # Stores the number of factorable integers. | |
local t # Value for checking divisibility. | |
local h # Copy of g for factoring. | |
for g in $FIBLIST | |
do | |
while (( c <= FIBLIMIT )) | |
do | |
h=$g | |
f=0 | |
for (( t=2; t<=h; t++ )) | |
do | |
while (( h % t == O )) | |
do | |
((f++)) | |
if (( $f > 1 )); then | |
break 2 | |
else | |
(( h /= t )) | |
fi | |
done | |
done | |
if (( $f == 1 )); then | |
echo $g | |
fi | |
((c++)) | |
break | |
done | |
done | |
} | |
_is_prime_number() | |
{ | |
unset PRIME FACT | |
local factor=() i num=$1 | |
for ((i=2; i<=num; i++)) | |
do | |
while ((num % i == O)) | |
do | |
factor+=($i) | |
((num /= i)) | |
done | |
done | |
case ${#factor[@]} in | |
1) PRIME=1 ;; | |
*) FACT="${factor[@]}" ;; | |
esac | |
} | |
_fibonacci_sequence() | |
{ | |
local a=0 b=1 sum | |
for (( i=0; i<${1:-10}; i++ )) | |
do | |
# echo $i: $a | |
echo $a | |
(( sum = a + b )) | |
a=$b | |
b=$sum | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment