I am making an app in visual basic with .NET Framework 4. I have to generate a list of prime numbers as per the user's input. So far, for my output if you put in 5 for the first five prime numbers you get 3 5 7 7 9 11 11. I am not sure if my number increment is in the wrong place. Thanks for any help you can give me. Also, I'm not sure how to include 2 as a prime number in my code.
Imports System.Math
Public Class Form1
Dim number, divisor, max, count As Integer
Dim IsPrime As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number = TextBox1.Text
For divisor = 2 To Sqrt(number)
If number Mod divisor = 0 Then
IsPrime = False
TextBox2.Text = ("Number is not prime")
Exit For
Else
TextBox2.Text = ("Number is prime")
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Wrap As String
Wrap = Chr(13) & Chr(10)
max = TextBox3.Text
Dim count = 0
number = 2
While count <= max
IsPrime = True
For divisor = 2 To Sqrt(number)
If number Mod divisor = 0 Then
IsPrime = False
Exit For
Else
IsPrime = True
TextBox4.Text += number & Wrap
count += 1
End If
Next
number += 1
End While
End Sub
End Class
You should not use the else branch in the for loop, in this case, each time the Mod unequal to 0, you will touch the else block, take number 11 for example:
11 mod 2 <> 0, you went into the else block,
11 mod 3 <> 0, you went into the else block again!
you can divide the number by all numbers between 2 to sqrt(number), and then use isprime to check whether it is a prime like this.(I am using VBScript here)
isprime = true
for i = 2 to Int(sqr(number))
if number mod i = 0 then
isprime = false
exit for
end if
next
if isprime = true then
count = count + 1
' do something here...
end if
number = number + 1
and don't forget to truncate the square root of number.
Just to share with you this script :
Option Explicit
Dim Title,Copyright,j,fso
Dim WshShell,Affich,LogFile
Title = "Calcul Nombres Premiers"
Copyright = " (c) by Hackoo 2015"
For j = 2 to 1000
If Premier(j) = True Then
Affich = Affich & j & vbTab
End If
Next
MsgBox Affich,vbInformation,Title + Copyright
LogFile = "c:\NombresPremiers.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(LogFile) Then
fso.DeleteFile LogFile
end If
Affich = replace(Affich,vbTab,vbCrlf)
Call WriteLog(Affich,LogFile)
Set WshShell=CreateObject("wscript.shell")
WshShell.Run LogFile
'**********************************
Function Premier(Nombre)
Dim i,d
' Trois nombres ne seront pas pris en compte par le compteur,
' on s'organise pour qu'ils soient vus avant.
Select Case Nombre
Case 0
Premier = False
Exit Function
Case 1
Premier = False
Exit Function
Case 2
Premier = True
Exit Function
End Select
For i = 2 To Int(Sqr(Nombre)) + 1
d = Nombre Mod i
If d = 0 Then
Premier = False
Exit Function
End If
Next
Premier = True
End Function
'**********************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'***********************************
Related
I am new to Visual Basic and I would like to ask how to do a loop until two numbers add up to 100. Basically, I want the user to keep entering the two numbers (via inputbox) until those two numbers add up to 100.
The current codes I have is as follow but it keeps crashing:
Public Sub Task2_B()
Do Until TotalWeight = 100
Do Until Val(weightA) = Int(Val(weightA)) And IsNumeric(weightA) And weightA <> "" And weightA > 0
weightA = Application.InputBox("Please enter weightA:", "Enter a positive integer", 100)
If weightA = False Then Exit Sub
Loop
Do Until Val(weightB) = Int(Val(weightB)) And IsNumeric(weightB) And weightB <> "" And weightB > 0
weightB = Application.InputBox("Please enter weightB:", "Enter a positive integer", 100)
If weightB = False Then Exit Sub
Loop
TotalWeight = Int(weightA) + Int(weightB)
Loop
Debug.Print TotalWeight
End Sub
I think your logic is redundant and repeated. Let's first define what valid input is once:
Public Function InputIsValid(ByVal theInput As String) As Boolean
InputIsValid = (Val(theInput) = Int(Val(theInput))) And Val(theInput) > 0
End Function
The first condition verifies that the input is a whole number. The second states it must be greater than zero. We know that it is numeric and not an empty string at this point so we don't need those additional tests.
Now we use that to validate our input:
Public Sub Task2B()
Dim TotalWeight As Long
Do Until TotalWeight = 100
Dim weightA As Variant
Do
weightA = InputBox("Please enter weightA:", "Enter a positive integer", 100)
If weightA = False Then Exit Sub
Loop Until InputIsValid(weightA)
Dim weightB As Variant
Do
weightB = InputBox("Please enter weightB:", "Enter a positive integer", 100)
If weightB = False Then Exit Sub
Loop Until InputIsValid(weightB)
TotalWeight = Val(weightA) + Val(weightB)
Debug.Print TotalWeight
Loop
End Sub
Notice I declared all the variables in use. I am also checking the state of the input bottom of the loop because it hasn't been set yet at the top.
Finally, we use Val to cast our string to a numeric every time we use it.
I explain one Y problem:
This is the call and diMatSE(i) can have different values, for example PRS02 and PRS03 for this example.
Call findCaMaterialsAndSumWeights(caMaterials, caMaterialsW, caMat, caMatW, diMatSE(i), diMatNotSE(i), i, posCaMaterialsTaken)
Here the definition of the arrays:
PRS02 = Array("201010", "207201", "213004", "210110")
PRS03 = Array("201010", "207201", "213004")
Here the summary sub:
Private Sub findCaMaterialsAndSumWeights(caMaterials As Variant, caMaterialsW As Variant, caMat As Variant, caMatW As Variant, diMatSE As Variant, diMatNotSE As Variant, y As Variant, posCaMaterialsTaken As Variant)
Select Case diMatSE
Case "PRS-02"
For i = LBound(PRS02) To UBound(PRS02)
Call posInTheArrayIgnoringPos(caMaterials, PRS02(i), posInArray, posCaMaterialsTaken)
If posInArray <> 0 Then 'If found one CA material that is a component from a Diko SE
numFound = numFound + 1
posCaMaterialsTaken(posInArray) = "x"
If caMatW(y) = "" Then
caMatW(y) = 0
End If
caMatW(y) = caMatW(y) + caMaterialsW(posInArray)
If numFound = UBound(PRS02) + 1 Then 'If all Diko SE materials are found in Diko materials
caMat(y) = "PRS-02"
For x = LBound(posCaMaterialsTaken) To UBound(posCaMaterialsTaken)
If posCaMaterialsTaken(x) = "x" Then 'Saving CA materials positions that compound a Diko SE
posCaMaterialsTaken(x) = 1
numFound = numFound - 1
If numFound = 0 Then
Exit For
End If
End If
Next x
End If
...
Else 'Not found one SE material
End If
Next i
Case "PRS-03"
(same code as PRS-02 case but PRS03 instead PRS02)
Case "PRS-04"
(same code as PRS-02 case but PRS04 instead PRS02)
...
Case else
Now I have several cases with the code repeated for the different values.
Solution to your Y-Problem:
You could use a dictionary to collect all the PRS-XX arrays.
Option Explicit
Dim PRS As Object
Sub Test()
Set PRS = CreateObject("Scripting.Dictionary")
'fill dictionary
PRS.Add "PRS-02", Array("201010", "207201", "213004", "210110")
PRS.Add "PRS-03", Array("201010", "207201", "213004")
'call it
findCaMaterialsAndSumWeights caMaterials, caMaterialsW, caMat, caMatW, diMatSE(i), diMatNotSE(i), i, posCaMaterialsTaken
End Sub
And then you could use it like PRS("PRS-02") to get the array Array("201010", "207201", "213004", "210110"). or even PRS("PRS-02")(1) to eg access item 1 of the array directly. If you now use your variable diMatSE = "PRS-02" like PRS(diMatSE) it takes the correct array according to your variable value.
So you only have the code once and can add as many PRS-xx to your dictionary as you want without touching this procedure again.
Private Sub findCaMaterialsAndSumWeights(caMaterials As Variant, caMaterialsW As Variant, caMat As Variant, caMatW As Variant, diMatSE As Variant, diMatNotSE As Variant, y As Variant, posCaMaterialsTaken As Variant)
For i = LBound(PRS(diMatSE)) To UBound(PRS(diMatSE))
Call posInTheArrayIgnoringPos(caMaterials, PRS(diMatSE)(i), posInArray, posCaMaterialsTaken)
If posInArray <> 0 Then 'If found one CA material that is a component from a Diko SE
numFound = numFound + 1
posCaMaterialsTaken(posInArray) = "x"
If caMatW(y) = "" Then
caMatW(y) = 0
End If
caMatW(y) = caMatW(y) + caMaterialsW(posInArray)
If numFound = UBound(PRS(diMatSE)) + 1 Then 'If all Diko SE materials are found in Diko materials
caMat(y) = diMatSE
For x = LBound(posCaMaterialsTaken) To UBound(posCaMaterialsTaken)
If posCaMaterialsTaken(x) = "x" Then 'Saving CA materials positions that compound a Diko SE
posCaMaterialsTaken(x) = 1
numFound = numFound - 1
If numFound = 0 Then
Exit For
End If
End If
Next x
End If
'...
Else 'Not found one SE material
End If
Next i
End Sub
I have to implement this code, but I'm not sure the syntax, I have problem with "ending an If sentence", first time that I've write a macro on VB.
Please any help to write this code well. I need to put the result of this in the Cell CK6:
(EDITED CODE)
Sub Funcion()
Dim listo As Boolean
Dim min As Integer
Dim max As Integer
Dim beta As Integer
Dim tolerancia As Long
Dim deficit As Long
listo = False
min = 0
max = 1
beta = 0
tolerancia = 0.000001
While listo = False
prom = (min + mas) / 2
deficit = Worksheets("ETR").Cells(6, "CI")
If (deficit > 0) Then
If (deficit < tolerancia) Then
beta = prom
listo = True
Else: min = prom
End If
Else
If (Abs(deficit) < tolerancia) Then
beta = prom
listo = True
Else: max = prom
End If
End If
Wend
End Sub
Is there something wrong now? The macro get stucked but at least it show no errors.
Helo guys. Could someone help me resolve my EXCEL VBA Code. I'm really having trouble on how to make my FLASHCARD appear IN ORDER from Cell A1 down to the last cell. I have this code but it only generates a RANDOM cell or value. How can I make it non random? I know that by using the code RND, it generates randomly. But how about NON RANDOM?
Here's the code:
Private Sub NextCard()
Application.ScreenUpdating = False
Dim finalTermRow As Integer
finalTermRow = Range("a60000").End(xlUp).Row
Dim possibleRow As Integer
Dim foundTerm As Boolean
foundTerm = False
Dim tries As Integer
tries = 0
Do While foundTerm = False And tries < 1000
possibleRow = Rnd() * (finalTermRow - 2) + 2
If Cells(possibleRow, 4).Value = "" Then
If possibleRow <> previousRow Then
foundTerm = True
End If
End If
tries = tries + 2
Loop
Application.ScreenUpdating = True
If tries < 1000 Then
currentRow = possibleRow
BoxQuestion.Text = Cells(currentRow, 1).Value
BoxDefinition.Text = ""
AltBox.Text = ""
Else
MsgBox ("There are no other cards to go to--you've learned everything else! Congratulations! To study all your cards again, click reset.")
End If
End Sub
Hope you could help me guys resolve this issue. Thank you.
replace:
tries = 0
Do While foundTerm = False And tries < 1000
possibleRow = Rnd() * (finalTermRow - 2) + 2
with
tries = 0
possibleRow=0
Do While foundTerm = False And tries < 1000
possibleRow = possibleRow + 1
' Gambas class file
' Math Drill by William Teder. Feel free to use parts of the code, but please give me credit.
' Declare Variables
' Define number of times user has pressed the Give Up button
PRIVATE givenuptimes AS Integer
' Define how many questions the user has answered
PRIVATE questionsanswered AS Integer
' Define what level the user is on
PRIVATE level AS Integer
' Define the number of points the user has
PRIVATE points AS Integer
' Define whether ot not addition, subtraction, multiplication, and division are enabled. This value is changed whrn the textboxes are togglrd.
PRIVATE additionenabled AS Boolean
PRIVATE subtractionenabled AS Boolean
PRIVATE multiplicationenabled AS Boolean
PRIVATE divisionenabled AS Boolean
'Set an integer for counting the number of times the program resets, for use in email.
PRIVATE resetcount AS Integer
' Define variable to help in detrmining the problem type
PRIVATE problemtype AS Integer
' Define number of numbers to add when using an addition problem
PRIVATE currentproblem AS String
PRIVATE currentanswer AS String
PRIVATE currentproblempointvalue AS Integer
PRIVATE add1 AS Integer
PRIVATE add2 AS Integer
PUBLIC SUB Form_Open()
'Set inital values
givenuptimes = 0
questionsanswered = 0
level = 1
points = 0
additionenabled = TRUE
subtractionenabled = TRUE
multiplicationenabled = TRUE
divisionenabled = TRUE
' GET RID OF THE FOLLOWING LINE WHEN USED IN BUILDING OR THE PROGRAM WILL NOT WORK!
' currentanswer = 0
makeproblem()
END
PUBLIC SUB btnClearAnswer_Click()
'clear the contents of txtAnswer and give it the focus
txtAnswer.Text = ""
txtAnswer.SetFocus
END
PUBLIC SUB chkGiveUp_Click()
' Check to see if the Give Up button's visible property is set to true, and if it is, hide the button. If it is hidden, show it again.
IF btnGiveUp.Visible = FALSE THEN
btnGiveUp.visible = TRUE
lblhGiveUp.visible = TRUE
lblGivenUp.visible = TRUE
expSettings.Raise
RETURN
END IF
IF btnClearAnswer.Visible THEN
btnGiveUp.Visible = FALSE
lblGivenUp.Visible = FALSE
lblhGiveUp.Visible = FALSE
expSettings.Raise
RETURN
END IF
END
PUBLIC SUB btnGiveUp_Click()
'Increment the counter that shows the number pf times the user has given up by 1
givenuptimes = givenuptimes + 1
lblGivenUp.Text = givenuptimes
'Display the right answer
txtAnswer.Text = currentanswer
WAIT 2
txtAnswer.Text = ""
points = points - 10
lblPoints.Text = points
makeproblem()
END
PUBLIC SUB chkLevels_Click()
' Check to see if the Level label's visible property is set to true, and if it is, hide the label. If it is hidden, show it again.
IF lblLevel.Visible = FALSE THEN
lblhLevel.Visible = TRUE
lblLevel.visible = TRUE
'Move the answered section down one place, if it is not already
lblhAnswered.Top = 150
lblAnswered.Top = 143
'For some odd reason, when the new objects appear they move themselves forward. Re-lowering the Settings container fixes this.
IF lblGivenUp.Visible = FALSE OR lblAnswered.Visible = TRUE THEN
lblhGiveUp.Top = 200
lblGivenUp.Top = 193
END IF
expSettings.Raise
RETURN
END IF
IF lblLevel.Visible THEN
lblLevel.Visible = FALSE
lblhLevel.Visible = FALSE
'Move the answered section up one place
lblhAnswered.Top = 105
lblAnswered.Top = 98
'Check to see if the GiveUp section needs to be moved
IF lblGivenUp.Visible = TRUE AND lblhLevel.Visible = FALSE THEN
lblhGiveUp.Top = 150
lblGivenUp.Top = 143
END IF
'See above comment
expSettings.Raise
RETURN
END IF
END
PUBLIC SUB btnReset_Click()
' Notify user the program is working
lblProblem.Text = "Resetting, please wait..."
'First, reset variables
givenuptimes = 0
questionsanswered = 0
level = 1
points = 0
'Clear the answer textbox.
txtAnswer.Text = ""
'Next, clear interface.
lblAnswered.Text = "0"
lblLevel.Text = "0"
lblPoints.Text = "0"
lblGivenUp.Text = "0"
' Notify user that the reset has finished and that the program is generating a new problem
lblProblem.Text = "Reset complete, generating new problem..."
makeproblem()
END
' These four subs are the same code with different variables. Changes state of variable the checkbox is assigned to, makes it the opposite of it's current state.
PUBLIC SUB chkAddition_Click()
IF divisionenabled = FALSE AND subtractionenabled = FALSE AND multiplicationenabled = FALSE THEN
PRINT Message("You must select at least one option.")
additionenabled = TRUE
chkAddition.Value = TRUE
RETURN
END IF
' Set the boolean additionenabled to be true or false based on it's current state
IF additionenabled = TRUE THEN
additionenabled = FALSE
RETURN
END IF
IF additionenabled = FALSE THEN
additionenabled = TRUE
RETURN
END IF
END
PUBLIC SUB chkSubtraction_Click()
IF additionenabled = FALSE AND divisionenabled = FALSE AND multiplicationenabled = FALSE THEN
PRINT Message("You must select at least one option.")
subtractionenabled = TRUE
chkSubtraction.Value = TRUE
RETURN
END IF
' Set the boolean subtractionenabled to be true or false based on it's current state
IF subtractionenabled = TRUE THEN
subtractionenabled = FALSE
RETURN
END IF
IF subtractionenabled = FALSE THEN
subtractionenabled = TRUE
RETURN
END IF
END
PUBLIC SUB chkMultiplication_Click()
IF additionenabled = FALSE AND subtractionenabled = FALSE AND divisionenabled = FALSE THEN
PRINT Message("You must select at least one option.")
multiplicationenabled = TRUE
chkMultiplication.Value = TRUE
RETURN
END IF
' Set the boolean multiplicationenabled to be true or false based on it's current state
IF multiplicationenabled = TRUE THEN
multiplicationenabled = FALSE
RETURN
END IF
IF multiplicationenabled = FALSE THEN
multiplicationenabled = TRUE
RETURN
END IF
END
PUBLIC SUB chkDivision_Click()
IF additionenabled = FALSE AND subtractionenabled = FALSE AND multiplicationenabled = FALSE THEN
PRINT Message("You must select at least one option.")
divisionenabled = TRUE
chkDivision.Value = TRUE
RETURN
END IF
' Set the boolean divisionenabled to be true or false based on it's current state
IF divisionenabled = TRUE THEN
divisionenabled = FALSE
RETURN
END IF
IF divisionenabled = FALSE THEN
divisionenabled = TRUE
RETURN
END IF
END
' Subroutine to make a problem
SUB makeproblem()
' This is the code that determines a new problem for the user based on a number of factors, including how many problems of that type they have done,
' whether or not the type is enabled or disabled, how quickly they can answer the question, thier points and level, and how many times they have
' given up on that type of problem. The rest is random. DO NOT MESS WITH THIS CODE UNLESS YOU KNOW WHAT YOU ARE DOING! It is very easy to mess up the program,
' as well as generate stack overflow errors.
' Engine version : 0.0.0.1
' Engine by William Teder
' ------------------------------------
' Generates a random number for the Problem Type, 1 - 4
problemtype = Rnd(1, 5)
IF problemtype = 1 THEN
' Problem Type: Addition
lblProblem.Text = "Problem Type: Addition"
' Determine the number of numbers to add together
IF level <= 5 THEN
' Determine how high the number should be
IF points <= 200 THEN
add1 = Rnd(1, 10)
add2 = Rnd(1, 10)
currentanswer = add1 + add2
currentproblem = add2 & " + " & add1
lblProblem.Text = currentproblem
RETURN
END IF
IF points > 200 AND < 400 THEN
add1 = Rnd(1, 20)
add2 = Rnd(1, 20)
add1 + add2 = currentanswer
currentproblem = add2 & " + " & add1
lblProblem.Text = currentproblem
RETURN
END IF
IF points > 400 AND < 500 THEN
add1 = Rnd(1, 30)
add2 = Rnd(1, 30)
add1 + add2 = currentanswer
currentproblem = add2 & " + " & add1
lblProblem.Text = currentproblem
RETURN
END IF
END IF
IF level <= 10 AND >= 6 THEN
' Code for three numbers
END IF
IF level <= 15 AND >= 11 THEN
'Code for 4 numbers
END IF
IF level <= 20 AND >= 16 THEN
' Code for 5 numbers
END IF
IF problemtype = 2 THEN
' Problem Type: Subtraction
lblProblem.Text = "Problem Type: Subtraction"
END IF
IF problemtype = 3 THEN
' Problem Type: Multiplication
lblProblem.Text = "Problem Type: Multiplication"
END IF
IF problemtype = 4 THEN
' Problem Type: Division
lblProblem.Text = "Problem Type: Division"
END IF
END
PUBLIC SUB gotright()
' Increment questions answered counter
questionsanswered = questionsanswered + 1
' Increment Points
points = points + currentproblempointvalue
' For every 100 points, increment the level counter by 1.
IF level = 1 AND points = 200 THEN level = 2
IF level = 2 AND points = 300 THEN level = 3
IF level = 3 AND points = 400 THEN level = 4
IF level = 4 AND points = 500 THEN level = 5
IF level = 5 AND points = 600 THEN level = 6
IF level = 6 AND points = 700 THEN level = 7
IF level = 7 AND points = 800 THEN level = 8
IF level = 8 AND points = 900 THEN level = 9
IF level = 9 AND points = 1000 THEN level = 10
IF level = 10 AND points = 1100 THEN level = 11
IF level = 11 AND points = 1200 THEN level = 12
IF level = 12 AND points = 1300 THEN level = 13
IF level = 13 AND points = 1400 THEN level = 14
IF level = 14 AND points = 1500 THEN level = 15
IF level = 15 AND points = 1600 THEN level = 16
IF level = 16 AND points = 1700 THEN level = 17
IF level = 17 AND points = 1800 THEN level = 18
IF level = 18 AND points = 1900 THEN level = 19
IF level = 19 AND points = 2000 THEN level = 20
' Change font color of the textbox to green to let the user know he/she got the problem right
txtAnswer.Foreground = &H579524&
' Create delay to let the user know they got the answer right
WAIT 1
' Change back to regular color
txtAnswer.Foreground = &HFF004&
END
PUBLIC SUB txtAnswer_KeyRelease()
IF txtAnswer.Text = currentanswer THEN
txtAnswer.Foreground = &H579524&
questionsanswered = questionsanswered + 1
lblAnswered.Text = questionsanswered
WAIT 0.25
txtAnswer.Text = ""
' Change back to regular color
txtAnswer.Foreground = &HFF0004&
lblPoints.text = points
makeproblem()
END IF
END
PUBLIC SUB Button1_Click()
makeproblem()
END
At line 251, there is an unexpected >. Why does it fail to compile? Thanks.
IF points > 400 AND < 500 THEN
That doesn't look quite right to me, unless your BASIC is really advanced (and it appears GAMBAS isn't quite that advanced). It should be:
IF points > 400 AND points < 500 THEN
You have the right idea with this line, for example:
IF lblGivenUp.Visible = TRUE AND lblhLevel.Visible = FALSE THEN
Same for all the variations you have of this, you need to include the variable on the right hand side of the and as well):
IF points > 200 AND < 400 THEN
IF points > 400 AND < 500 THEN
IF level <= 10 AND >= 6 THEN
IF level <= 15 AND >= 11 THEN
IF level <= 20 AND >= 16 THEN