I am writing my own script for a simple calculator that determines if a number is prime, and its prime factors. Currently my code looks like: - Private Sub primebutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles primebutton.Click
-
Dim oWrite As System.IO.StreamWriter
-
oWrite = File.CreateText("C:\TempFactors.txt")
-
TestLabel.Text = ""
-
'Determines perfect square qualifications on following 5 lines
-
Dim root As Double
-
Dim compare As Integer
-
root = System.Math.Sqrt(answer)
-
compare = CInt(CLng(root) \ 1)
-
If root = compare Then TestLabel.Text = "Perfect Square Composite (Not Prime)"
-
'Determines if number is prime
-
Dim n1 As Double
-
Dim n2 As Double
-
Dim n3 As Double
-
Dim loopcount As Double
-
n3 = compare / 1
-
loopcount = compare - 2
-
Do Until loopcount = 0
-
n1 = answer / n3
-
n2 = CLng(n1) \ 1
-
If n1 = n2 Then oWrite.Write(n2.ToString, n3.ToString)
-
End
-
loopcount = loopcount - 1
-
Loop
-
oWrite.Close()
-
End Sub
The loopcount = compare - 2 means I want the loop to be 2 less than the square root of the answer so that when do a descending calculation (loopcount = loopcount - 1) it doesn't see if 1 or 0 is a factor of the answer. for the calculations after that, I want, every time n1 = n2 (a whole number factor of the answer), I want to report those results to a text file (n2 and n3) in this format "(n2, n3)" then go to the next line. and continue to execute the loop until the loopcount is zero. Currently, when I run the code, the application closes when I click the prime button (button that runs this code) and I don't know why. This is my first semester in school, and have never coded before, so I am a definite laymen with code, but I really really want to learn it (hence why for fun im just creating algorithms for different things). Please help!! What did I do wrong and how can I fix this problem. Thanks in advance.
-Nick
2 1239 tlhintoq 3,525
Recognized Expert Specialist
But I can make some suggestions. - Assume that everything is broken, or at least not ideal.
- Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
- Assume that hardware breaks in the middle of what you are doing, so you have to recover.
- Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
- Check that folders exist, even if you just did a call to make it: You may not have permissions.
- Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
- Put a breakpoint on line 1.
- When the code stops there, walk through line by line with F-10.
- Check the values of your assumptions.
- Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
- Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.
tlhintoq 3,525
Recognized Expert Specialist TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out. Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Follower |
last post by:
Hi,
I am working on a function to return extracts from a text document
with a specific phrase highlighted (i.e. display the context of the
matched phrase).
The requirements are:
* Match should be case-insensitive, but extract should have case
preserved.
|
by: Winston |
last post by:
Hi,
I want to use the Webbrowser Control (the Internet Explorer Control) to
display an XML-structure within a C# WinForm.
The way the IE displays a XML-file in its native form is exactly what I need
(as tree structure with plus/minus).
This works great as long as I load the XML to display from a file with the
Navigate2-method of the control.
But my XML comes not from a file but is created dynamically during runtime.
|
by: Mark |
last post by:
Hello all,
I have the following file.txt storing these kind of values:
Name: John Doe
E-mail: john@doe.com
Phone: 555 865 8901
Address: 71 Huntington Ln. (33444) FL - Delray Beach US
Member ID: 373732510149005
Registration date: 5/2005
|
by: BluNuit |
last post by:
I have a simple c# app that calls a Java (AXIS) web service to perform some
operations. The call works fine (the java code fires and the operations are
executed), but the response is always null. Other applications (besides the
c# ones) are able to execute the web service and get the proper response.
I put a SOAP sniffer on the call and the response was:
193
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope...
|
by: Jono |
last post by:
Hi Everyone,
As it says in the title, I'm looking for a way to display a page while
long running operations are performed on the server. Ideally, I'd like
some way to push the current request onto some stack, where it would
continue to be processed asynchronously (most importantly preserving
things like view state, form post data, etc). In the interim, while the
main request is processed, a friendly page will be displayed to the
user....
| |
by: murrayatuptowngallery |
last post by:
I started a JS project (my first) a year or two ago, got distracted &
am just now getting back to it.
By luck I had somehow found a way to collect data with a form, perform
calculations with JavaScript and display the form and calculated
results on the same screen.
Now I can't figure out how to do that again. The only methods of
reporting the calculation results seem to display a completely new
screen/window.
|
by: aziziahmad |
last post by:
hello..i'm trying to display my result in text file..
how am i going to display from printf command to display at
text file?
this is the example of of line need to be display in text file =
printf("Source address ::: %s\n",inet_ntoa(from.sin_addr)
i want to display this result in text file..
|
by: Chris McFarling |
last post by:
What's the most efficient method to export the result set of a SQL query to
a text file using ASP.NET? There could potentially be a few thousand rows.
|
by: tshad |
last post by:
I have a Windows App that is doing some work and then writing a "Now
Processing..." line to the status line of the window as well as the Textbox
on the form.
But the problem is that the work is in another class from the main class.
So it couldn't access the Status Line or textbox.
So what we did was set them up as properties:
string IStatusDisplay.Status
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |