lolテ2￱1テ3&#6戴尔14r5437拆机教程;1テ4ᅪ1ツ7&#

Find Prime Numbers & Store Them In An Array (List)
Hello There, Guest!
[Source]&Find Prime Numbers & Store Them In An Array (List)
0 Vote(s) - 0 Average
Posts: 9,688
Threads:1,013
Joined: Jun 2011
Reputation:
Find Prime Numbers & Store Them In An Array (List)
Here's a cool little snippet I wrote based on an idea that I seen from a C++ code floating around on another forum. This bit of code will find all prime numbers within a specified range of numbers, and write them to a text file.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
&&&&Dim t As New Thread(AddressOf StartThreadedSearch)
&&&&t.IsBackground = True
&&&&t.Start()
Private Sub StartThreadedSearch()
&&&&If InvokeRequired Then
&&&&&&&&Invoke(New MethodInvoker(AddressOf StartThreadedSearch))
&&&&&&&&Exit Sub
&&&&End If
&&&&Using sw As New StreamWriter(MyFilePathHere, True)
&&&&&&&&For Each Num As Long In FindPrimesInRange(0, 10000)
&&&&&&&&&&&&sw.WriteLine(Num.ToString)
&&&&&&&&Next
&&&&End Using
&&&&MessageBox.Show("Done")
Private Function FindPrimesInRange(RangeStart As Long, RangeEnd As Long) As List(Of UInt32)
&&&&If RangeEnd & RangeStart Then Throw New Exception("RangeEnd is less than RangeStart param")
&&&&Dim PrimeArray As New List(Of UInt32)
&&&&'2 is a prime number
&&&&If RangeStart &= 2 AndAlso RangeEnd &= 2 Then
&&&&&&&&PrimeArray.Add(2)
&&&&&&&&RangeStart = 2
&&&&End If
&&&&If RangeStart Mod 2 = 0 Then RangeStart += 1
&&&&For i As UInt32 = CUInt(RangeStart) To CUInt(RangeEnd) Step 2
&&&&&&&&If i = 3 OrElse i = 5 Then
&&&&&&&&&&&&PrimeArray.Add(i)
&&&&&&&&Else
&&&&&&&&&&&&Dim PrimeNum As Boolean = True
&&&&&&&&&&&&For x As UInt32 = 3 To CUInt((i \ 2))
&&&&&&&&&&&&&&&&If x && i Then
&&&&&&&&&&&&&&&&&&&&If i Mod x = 0 Then PrimeNum = False
&&&&&&&&&&&&&&&&End If
&&&&&&&&&&&&&&&&If x = (i \ 2) AndAlso PrimeNum = True Then
&&&&&&&&&&&&&&&&&&&&PrimeArray.Add(i)
&&&&&&&&&&&&&&&&End If
&&&&&&&&&&&&Next
&&&&&&&&End If
&&&&Return PrimeArray
End Function
This speeds up the process by minimizing the searching that it does by more than half... *Could optimize it more, but this is pretty much pointless anyways.
This post was last modified: 02-06- AM by .
Microsoft MVP .NET Programming - (2012 - Present)
(R)Crestron DMC-T Certified Automation Programmer
Development Site:
Posts: 9,688
Threads:1,013
Joined: Jun 2011
Reputation:
RE: Find Prime Numbers & Store Them In An Array (List)
Here's the output from 0 - 10000:
Microsoft MVP .NET Programming - (2012 - Present)
(R)Crestron DMC-T Certified Automation Programmer
Development Site:
Posts: 9,688
Threads:1,013
Joined: Jun 2011
Reputation:
RE: Find Prime Numbers & Store Them In An Array (List)
Feel free to add to this if you can, I was just looking for some cool projects to try out and present for a new system soon to be at TLF for learning VB :)
Microsoft MVP .NET Programming - (2012 - Present)
(R)Crestron DMC-T Certified Automation Programmer
Development Site:
Posts: 9,688
Threads:1,013
Joined: Jun 2011
Reputation:
RE: Find Prime Numbers & Store Them In An Array (List)
I just went through 0 - 100 000 range But I think it could be split up so that the array doesn't hold all the values at once, for example output an array holding the values every difference of 10000 in the total range to the file at a time. That could speed it up...
Microsoft MVP .NET Programming - (2012 - Present)
(R)Crestron DMC-T Certified Automation Programmer
Development Site:
Forum Jump:
--------------------
Private Messages
User Control Panel
Who's Online
Forum Home
Rules & Announcements
Site Suggestions, Ideas, & Bug Reports
The Welcome Area
Registration Problems?
Reboot Challenges
Programming and Development
.Net Framework Programming
---- .Net Showcase
General Programming Discussion
---- Developer Ideas for Programs & Applications
Web Development & Web Graphics
C/C++/Objective-C Based Programming
Computer Support
Windows Operating Systems
Apple Support
Linux Support
Malware, Spyware and Virus Help
---- General Malware Discussion
Hardware Or Software Discussion & System Crash Support
Web Browser & General Internet/Networking Discussion
Off Topic Chat
Game, Music, & Other Electronics/Media Discussion
Group Recruitment
Users browsing this thread: 1 Guest(s)
MyBB Default
MetroStyle
MetroStyleMobile[text]关灯返回顶部&>&【第3回MMD杯本選】キャプテン翼OPをかっこよ
提示请下载最新的flash插件拖动播放器【第3回MMD杯本選】キャプテン翼OPをかっこよsm7953976&看着揪心的运动&后面40多秒是补丁分享收藏0在线0弹幕0香蕉0
sm7953976&看着揪心的运动&后面40多秒是补丁 简介sm7953976&看着揪心的运动&后面40多秒是补丁[+展开简介]
官方下载功能反馈本站不提供任何视听上传服务,所有内容均来自视频分享站点所提供的公开引用资源。Copyright (C)
AcFun. 保留所有权利歌曲 六兆年と一夜物Z 歌手 赤ティン
热卖大片12345678910最新上映12345678910
日(金)10:41 踊陡ほんとにほんとにほんとにほんとにご苦氦丹螅デデドンケロケロMIXなるものに挑椁筏蓼筏俊K激い韦郅饯欷盲荬胜盲郡人激い蓼埂あとは某まふなんとかさんがやってるようなみじん切り?にも挑椁筏蓼筏俊
10 人认为非常好听
被评为“好听”的歌将被顶到首页第一个位置
六兆年と一夜物Z
第一次推荐  
相关专辑:

我要回帖

更多关于 14r 5437 的文章

 

随机推荐