vb.net - Prime Number Visual Basic -
i have assignment , says:
you have been provided working program named prime.
has 1 input single integer.
when “find prime number” button pushed, nth prime number identified.
for example:
if 4 entered , button clicked, response “the 4th prime number 7” displayed. , if 9 entered, response “the 9th prime number 23” displayed.
the program 100% accurate, correct locates nth prime.
problem comes in when try find larger prime number.
for example:
if 10000 entered , button clicked, response “the 10000th prime number 104729” displayed. correct answer; however, took on 48 seconds on i7 computer find solution. imagine how long take find millionth prime number. task analyze problem find more efficient solution make program more useful. first must understand how code provided works. there exercise @ end of document. use run code hand. once understand how works, analyze problem of finding prime number make program operate more efficiently. reason why program slow large numbers because preform lot of unnecessary calculations. not must use own code. cannot use prebuilt prime number routines, functions, or libraries; doing result in 0 assignment. thinking needs done way solve this.
he says best way use arrays, don't know how use arrays solve problem. says keep division (mod) @ minimum if statements.
here code provided us:
option strict on public class form1 private sub positionbox_textchanged(byval sender system.object, byval e system.eventargs) handles positionbox.textchanged if isnumeric(positionbox.text) if decimal.parse(positionbox.text) >= 1 findprimebtn.enabled = true else findprimebtn.enabled = false end if else findprimebtn.enabled = false end if end sub private sub findprimebtn_click(byval sender system.object, byval e system.eventargs) handles findprimebtn.click dim finalposition long dim finalprime, long dim number long = 2 dim currentposition long = 1 dim elapsed system.timespan dim sw stopwatch = new stopwatch() sw.start() 'starts clock dim isprime boolean finalposition = long.parse(positionbox.text) = currentposition mod 2 while (currentposition > 2 , <> 0) isprime = true end while while (currentposition <= finalposition) isprime = true x = 2 number - 1 if number mod x = 0 isprime = false end if next if isprime finalprime = number currentposition += 1 end if number += 1 end while elapsed = sw.elapsed() 'captures elapsed time took compute result resultlbl.text = "the " + finalposition.tostring() + "th " + finalprime.tostring() elapsedtimelbl.text = "elapsed time " + elapsed.tostring() end sub end class
check out this article , specifically, says: effort can reduced selecting prime numbers candidate factors. furthermore, trial factors need go no further \scriptstyle\sqrt{n} because, if n divisible number p, n = p × q , if q smaller p, n have earlier been detected being divisible q or prime factor of q.
i think wants store primes find in array , iterate through set of primes in array instead of iterating through each or each odd number.
not sure if he'll allow import system.math, square root function is, speeds up. suppose ask...
anyways, i'm not using stored array of found prime numbers, , didn't variable names, if helps, working this:
option strict on imports system.math public class form1 private sub positionbox_textchanged(byval sender system.object, byval e system.eventargs) handles positionbox.textchanged if isnumeric(positionbox.text) if decimal.parse(positionbox.text) >= 1 findprimebtn.enabled = true else findprimebtn.enabled = false end if else findprimebtn.enabled = false end if end sub private sub findprimebtn_click(byval sender system.object, byval e system.eventargs) handles findprimebtn.click dim currentprimesequence long = 1 dim finalprimesequence long dim prime_possible double = 2 dim foundprime double dim test_div double 'dim long dim elapsed system.timespan dim sw stopwatch = new stopwatch() sw.start() 'starts clock dim isprime boolean finalprimesequence = long.parse(positionbox.text) 'even = currentprimesequence mod 2 'while (currentprimesequence > 2 , <> 0) ' isprime = true 'end while findprimebtn.enabled = false while (currentprimesequence <= finalprimesequence) isprime = true ' until proven false test_div = 2 while isprime , (test_div <= sqrt(prime_possible)) isprime = (prime_possible mod test_div > 0) test_div = test_div + 1 - cdbl(test_div > 2) ' skip odd numbers after 2 'if test_div = 2 ' test_div = test_div + 1 'else ' test_div = test_div + 2 'end if loop 'for test_div = 2 sqrt(prime_possible) ' test if divisible number 2 square root of candidate, skip #s ' if prime_possible mod test_div = 0 ' isprime = false ' exit ' or change while isprime , (test_div < prime_possible) ' end if 'next if isprime foundprime = prime_possible if currentprimesequence mod 100000 = 0 debug.print(cstr(currentprimesequence) + " " + cstr(foundprime) + " " + sw.elapsed().tostring) end if currentprimesequence += 1 end if prime_possible += 1 end while elapsed = sw.elapsed() 'captures elapsed time took compute result resultlbl.text = "the " + finalprimesequence.tostring() + "th " + foundprime.tostring() elapsedtimelbl.text = "elapsed time " + elapsed.tostring() findprimebtn.enabled = true end sub end class
on pc, found millionth prime in 28 seconds, can't compare performance across machines.
Comments
Post a Comment