Help with ahem Prime number Generating Script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with ahem Prime number Generating Script
# 1  
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.Smilie
Iam using Cygwin on Vista
Thanks
(Indented a bit betterSmilie)
Code:
 
#!/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

Smilie

Last edited by drewann; 12-16-2012 at 04:16 PM.. Reason: additional info
# 2  
A small suggestion - indent your code corrected so that people could more easily understand it.
This User Gave Thanks to fpmurphy For This Post:
# 3  
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.
This User Gave Thanks to Yoda For This Post:
# 4  
Further details

Indented a bit better
Sorry I was a bit scant on details.Smilie
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.Smilie

Last edited by drewann; 12-16-2012 at 04:19 PM.. Reason: additional info
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #375
Difficulty: Easy
In December 2008, the Austin Group published a new major Unix revision, known as POSIX:2008 (formally: IEEE Std 1013.1-2008).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

2. Shell Programming and Scripting

Generating a POSIX random number?

Hi Guys and gals... As you know I am getting to grips with POSIX and hit this stumbling block. Generating two random numbers 0 to 255 POSIXly. Speed in not important hence the 'sleep 1' command. I have done a demo that works, but it sure is ugly! Is there a better way? #!/bin/sh # Random... (12 Replies)
Discussion started by: wisecracker
12 Replies

3. Shell Programming and Scripting

Random number generating script?

Having a hard time with this. Very new to scripting and linux. Spent all sunday trying to do this. Appreciate some help and maybe help breaking down what the syntax does. Create a Bash program. It should have the following properties • Creates a secret number between 1 and 100 i. The... (3 Replies)
Discussion started by: LINUXnoob15
3 Replies

4. Shell Programming and Scripting

Generating Random Number in certain range

Hi there I am trying to generate a random number between 40 and 70 using the shell here is my code so far and it keeps going above 70. all help much appreciated! comp=$(( RANDOM%70+40 )) echo $comp (4 Replies)
Discussion started by: faintingquiche
4 Replies

5. Shell Programming and Scripting

Generating graphs for many number of files

Hi, I have a series of data files for which I wish to plot using "splot". Say, the files names are like: 950_data,951_data,952_data,......1000_data. For one file , say to plot 950_data, i write following lines into a single file and load it in the gnuplot as : gnuplot> load 'plot' ... (6 Replies)
Discussion started by: begin_shell
6 Replies

6. Shell Programming and Scripting

Perl Prime number help

Hello, I have some extra time at work and I'm trying to come up with a good prime number generator for very large numbers that can take advantage of multiple (hundreds) cores. I realize Perl may not be the best solution for this, any ideas? Thanks! (1 Reply)
Discussion started by: Kweekwom
1 Replies

7. Programming

C Help; generating a random number.

Im new to C, and Im having a hard time getting a random number. In bash, I would do something similar to the following to get a random number; #!/bin/bash seed1=$RANDOM seed2=$RANDOM seed3=$RANDOM SEED=`expr $seed1 * $seed2 / $seed3` echo ${SEED%.*} Now, in online examples... (4 Replies)
Discussion started by: trey85stang
4 Replies

8. Programming

generating 16 digit random number in C

Hi, How can we generate 16 digit random nos in C. (10 Replies)
Discussion started by: ajaysahoo
10 Replies

9. UNIX for Dummies Questions & Answers

Generating line number

Hi, I am generating a file through some Datastage commands: cat $TempDir/stage.txt |while read line do stagename=`echo $line` dsjob -llinks $proj $jobname $stagename 2>/dev/null >> $TempDir/LinkName.txt Now i have to assign the number... (5 Replies)
Discussion started by: Amey Joshi
5 Replies

10. Shell Programming and Scripting

Prime Number Program (Fun)

Hi, I was just wondering if anyone has, or knows where to download a prime number finder program. I would like a fairly simple bash program, and also I would like one that could take advantage of multiple processors. I have 500 cores I can use, and would like to take advantage of them using a... (2 Replies)
Discussion started by: Kweekwom
2 Replies

Featured Tech Videos