Help with ahem Prime number Generating Script

Can anybody tell me why the second part of this script (Sieve of Eratosthenes) isn't working properly. This isnt coursework or homework just private studies ( Yes Project Euler began it ) I know there are easier ways of doing this too but I want to do it this way.:stuck_out_tongue:
Iam using Cygwin on Vista
Thanks
(Indented a bit better:o)

 
#!/bin/bash
#------PERFORM WHEEL FACTORISATION------
echo "WHEEL FACTORISATION"
let i=0
let j=0
let filenamy=1
for Input_Number in {1..1000}
          do
# Write each 6 numbers of sequence to new temp txtfiles
if [ $i == 6 ]
          then
          filenamy=$(($filenamy+1))
          let i=0
          fi
# Output each number to a newline
          LINE1=$Input_Number
          FILE=${LINE1}
          echo -e $FILE>>/home/Gio/OutputtedFiles/Filenammies/$filenamy.txt
          i=$(($i+1))
          done
# Count the number of txtfiles to $filecounter1
cd /home/Gio/OutputtedFiles/Filenammies
let Filecounter1=`ls *.txt | wc -l` 
for (( filenamy=1; filenamy <= $Filecounter1; filenamy++ ))
          do
          # SED lines to temp txtfiles
          LINE2=`sed -n '1p' /home/Gio/OutputtedFiles/Filenammies/$filenamy.txt`
          LINE2a=`sed -n '3p' /home/Gio/OutputtedFiles/Filenammies/$filenamy.txt`
          LINE2b=`sed -n '5p' /home/Gio/OutputtedFiles/Filenammies/$filenamy.txt`
          FILE2=${LINE2}
          FILE2a=${LINE2a}
          FILE2b=${LINE2b}
          echo -e $FILE2>>/home/Gio/OutputtedFiles/oUtPuT.txt
          echo -e $FILE2a>>/home/Gio/OutputtedFiles/oUtPuT.txt
          echo -e $FILE2b>>/home/Gio/OutputtedFiles/oUtPuT.txt
          done
# Count the number of txtfiles to $filecounter1
let Filecounter1=`ls *.txt | wc -l` 
for (( filenamy=1; filenamy <= $Filecounter1; filenamy++ ))
          do
          # remove temporary txtfiles 
          rm /home/Gio/OutputtedFiles/Filenammies/$filenamy.txt
          done
 

# ------LOOPS TO RUN Sieve of Eratosthenes------
echo "Sieve of Eratosthenes"
# Calculate sqrt and round down resulting number
lastwoot=`cat /home/Gio/OutputtedFiles/oUtPuT.txt | grep "." | tail -1` 
calc=$(echo "sqrt ( $lastwoot )" | bc -l | xargs printf "%1.0f")
let calc1=$( expr $calc + 1 )
for anyname in {1..10000}
          do
          if [ $LINE2 == $calc1 ]
          then
          break
          fi
a=$anyname"p" 
LINE1=`sed -n $a /home/Gio/OutputtedFiles/oUtPuT.txt` 
FILE1=${LINE1}
let Wordcounter=`cat /home/Gio/OutputtedFiles/oUtPuT.txt|wc -w` 
let a=$( expr $Wordcounter + 1 )
for b in {1..1000}
          do
          if [ $b == $a ]
          then
          break
          fi
a=$b"p" 
LINE2=`sed -n $a /home/Gio/OutputtedFiles/oUtPuT.txt`
FILE2=${LINE2}
# Delete multiples of X  
y=`expr $FILE1 % $FILE2`
if [ $y == 0 ]
          then      
          delly=$b"d"
          sed -i $delly /home/Gio/OutputtedFiles/oUtPuT.txt
          fi
echo "$FILE1 file 1"
echo "$FILE2 file 2"
# Keep prime numbers
if [ $FILE1 == $FILE2 ]  
          then      
          echo $FILE2>>/home/Gio/OutputtedFiles/oUtPuT.txt
          fi
# Sort numbers because primes will be tagged onto end
sort -n /home/Gio/OutputtedFiles/oUtPuT.txt
# End of Loop Two
done
# End of Loop One
done

:slight_smile:

A small suggestion - indent your code corrected so that people could more easily understand it.

1 Like

First of all you did not clearly mention how Sieve of Eratosthenes is not working properly. What issues or errors you are observing while running.

But I looked at your Sieve of Eratosthenes and found several lines with Useless Use of Cat which you can avoid. Also here are few corrections that you can make and re-run:-

Replace let calc1=`expr $calc+1` with let calc1=$( expr $calc + 1 ) (should have space b/w operators and operands)

Replace let a=`expr $Wordcounter+1` with let a=$( expr $Wordcounter + 1 ) (should have space b/w operators and operands)

Replace == with -eq while performing numeric comparison.

1 Like

Indented a bit better
Sorry I was a bit scant on details.:smiley:
Well for one when I ran it and it prints out the list of sieved numbers 999 still appears which should have been sieved earlier on by (999 % 3)
Also the number five didnt appear in the list.
Also after it has ran through the (prime % numbertobechecked)
expr: syntax error
./TestFive: line 82: [: ==: unary operator expected
1 file 1
file 2
./TestFive: line 90: [: 1: unary operator expected

After adding an echo to see the values of $FILE1 and $FILE2 it appears that the value of $FILE1 is always 1...

Sorry if the details are a bit scatty my brain has been dumbed down by a couple of hours on facebook.:stuck_out_tongue: