amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 31 May 2015 04:44 PM |
I think it's good. In practical uses I'd return a table, but I'm starting off with printing them directly to make it more visual.
local isPrime = function(number) local prime = true for i = 2, number - 1 do if number % i == 0 then prime = false break end end return prime end
local getPrimeNumbersUpTo = function(number) for i = 1, number do if isPrime(i) then print(i) end end end
getPrimeNumbersUpTo(100)
--[[Output: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 --]] |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 31 May 2015 04:46 PM |
function isPrime(n) return n == 2 or (n%2 ~= 0 and n%3 ~= 0 and n%5 ~= 0 and n%7 ~= 0) end |
|
|
| Report Abuse |
|
|
| |
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 31 May 2015 04:48 PM |
| What would be a better way to do it besides storing already known prime numbers? |
|
|
| Report Abuse |
|
|
LucasLua
|
  |
| Joined: 18 Jun 2008 |
| Total Posts: 7386 |
|
|
| 31 May 2015 04:48 PM |
| You should do some research; there are plenty of algorithms you could use to generate them faster. |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 31 May 2015 04:49 PM |
| Hmp. Didn't know, I just thought I'd try a shot at it. Didn't do any research at all. I suppose you could consider this a first step though. |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 04:49 PM |
| Google Sieve of Eratosthenes. |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 31 May 2015 04:51 PM |
*feels more stupid the more i read about what i could of done better*
could of skipped all the even numbers
doh |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 04:54 PM |
| This may interest you: http://www.wikiwand.com/en/Sieve_of_Atkin |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 31 May 2015 04:55 PM |
| My computer can't handle trying to get the prime of 100,000. Took about 5 minutes for studio to respond after calculating up to 100k. Lol |
|
|
| Report Abuse |
|
|
LucasLua
|
  |
| Joined: 18 Jun 2008 |
| Total Posts: 7386 |
|
|
| 31 May 2015 04:56 PM |
You shouldn't be using roblox studio for this kind of computational work. Lol
Try the Lua for Windows interpreter, it should be on Google Code somewhere. |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 04:57 PM |
| ZeroBaneStudio is also good. |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 04:58 PM |
| http : / / en . wikipedia . org / wiki / Primality _ test |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 31 May 2015 05:01 PM |
| Oh. I don't plan to try to find a really big prime number or anything, that's not the point of this. I'm just messing around. |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 05:03 PM |
Their are better ways of doing but this is a first time and I bet your learning scripting. So its all good.
Now how about get a random number and try to figure out if its odd or even and must bee under 10 lines :) |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 05:04 PM |
^ omg that sounds rlly difficlt :( |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 31 May 2015 05:09 PM |
| I bet your learning scripting [2] |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 05:10 PM |
| I bet your learning scripting [3] |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 31 May 2015 05:11 PM |
| We're all learning scripting |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 05:11 PM |
function IsPrime(n) for i = 1, n, 2 do if i > math/sqrt(n) then return true end if n%i==0 then return false else table.insert(Repeats, i) end end end
If I made an error in my above logic please feel free to correct me :) |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 05:13 PM |
print((function IsEven(n) return n%2==0 end)(math.random(1, 100000)))
Legit 1 line. |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 05:14 PM |
print((function(n) return n%2==0 end)(math.random(1, 100000)))
Legit 1 line. |
|
|
| Report Abuse |
|
|
|
| 31 May 2015 05:15 PM |
I don't know why I left the IsEven function, I meant to take that out altogether.
print(math.random(10000)%2==0) |
|
|
| Report Abuse |
|
|
adandee
|
  |
| Joined: 20 Feb 2014 |
| Total Posts: 231 |
|
|
| 31 May 2015 05:16 PM |
Wonder if it is possible to make the prime numbers generate something if it is that number.
Like this
if Num.Value = 17 then m = Instance.new('Message', workspace) m.Text = 'Mynamejeff' end
|
|
|
| Report Abuse |
|
|