Advertisement
Daraketh

PrimeCount

Aug 25th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.09 KB | None | 0 0
  1. local Prime = 1
  2. local i
  3. local count
  4. local primeCount
  5. local num = 1
  6.  
  7. side = "right"
  8. term.redirect(peripheral.wrap(side))
  9.  
  10. print("Welcome to my prime counting program. This program will display all numbers between 1 and the user defined number, making a note next to all prime numbers. Enter a value less than or equal to 0 to quit.")
  11.  
  12. while num > 0 do
  13.  
  14.     print("")
  15.     print("Please input your number: ")
  16.     num = read()
  17.    
  18.     primeCount = 0
  19.    
  20.     for count=1,num,1 do
  21.        
  22.         Prime = 1
  23.        
  24.         if count == 1 then
  25.        
  26.             Prime = 0
  27.            
  28.         end
  29.        
  30.         if(count ~= 2 and Prime == 1)then
  31.            
  32.             if(math.fmod(count,2) == 0)then
  33.            
  34.                 Prime = 0
  35.                
  36.             end
  37.            
  38.             for i=3,count,2 do
  39.            
  40.                 if(math.fmod(count,i) == 0)then
  41.                    
  42.                     Prime = 0
  43.                
  44.                 end
  45.            
  46.             end
  47.            
  48.         end
  49.        
  50.         if Prime == 1 then
  51.        
  52.             primeCount = primeCount + 1
  53.             print(count .. " ---- " .. primeCount .. "th Prime")
  54.            
  55.         else
  56.        
  57.             print(count)
  58.            
  59.         end
  60.        
  61.         if count == num then
  62.        
  63.             print("")
  64.             print("There are" .. primeCount .. " prime numbers between 1 and " .. num)
  65.            
  66.         end
  67.        
  68.     end
  69.  
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement