If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
my answer is coming 21148. i cant find the mistake here is my code in vb
the answer should be 21124
Dim length As UInt32 = 11
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 999
If i / 10 < 1 Then
single_digit(i)
ElseIf i / 10 = 1 Then
ten()
ElseIf i / 10 > 1 And i / 10 < 2 Then
ele_twe_thi(i)
ElseIf i / 10 >= 2 And i / 10 < 10 Then
multiple_10(Math.Floor(i / 10) * 10)
single_digit(i Mod 10)
ElseIf i = 100 Then
single_digit(i / 100)
length += 7
ElseIf i Mod 100 >= 11 And i Mod 100 <= 19 Then
length += 10
single_digit(Math.Floor(i / 100))
ele_twe_thi(i Mod 100)
ElseIf i Mod 100 = 10 Then
length += 3
single_digit(Math.Floor(i / 100))
length += 10
Else
length += 10
single_digit(Math.Floor(i / 100))
multiple_10(Math.Floor((i Mod 100) / 10) * 10)
single_digit(i Mod 10)
End If
Next
MsgBox(length)
End Sub
Private Sub single_digit(num)
If num = 1 Then
length = length + 3
ElseIf num = 2 Then
length = length + 3
ElseIf num = 3 Then
length = length + 5
ElseIf num = 4 Then
length = length + 4
ElseIf num = 5 Then
length = length + 4
ElseIf num = 6 Then
length = length + 3
ElseIf num = 7 Then
length = length + 5
ElseIf num = 8 Then
length = length + 5
ElseIf num = 9 Then
length = length + 4
End If
End Sub
Private Sub ele_twe_thi(num)
If num = 11 Or num = 12 Then
length = length + 6
ElseIf num = 13 Or num = 14 Or num = 18 Or num = 19 Then
length = length + 8
ElseIf num = 17 Then
length = length + 9
ElseIf num = 15 Or num = 16 Then
length = length + 7
End If
End Sub
Private Sub multiple_10(num)
If num = 20 Then
length = length + 6
ElseIf num = 30 Then
length = length + 6
ElseIf num = 40 Then
length = length + 5
ElseIf num = 50 Then
length = length + 5
ElseIf num = 60 Then
length = length + 5
ElseIf num = 70 Then
length = length + 7
ElseIf num = 80 Then
length = length + 6
ElseIf num = 90 Then
length = length + 6
End If
End Sub
Private Sub ten()
length = length + 3
End Sub
every time the program reaches 100, 200, 300 etc. is it adding an "and" unnecessarily?
Related
Make a program that reads a positive integer no greater than 10, and prints a triangle of numbers as follows:
If the number read were 5, then it should print:
1
22
333
4444
55555
The program must reread another number until the number entered is greater than 10.
I have tried this, but I don't know if it is correct:
Dim num As Integer
Dim i As Integer
num = InputBox(" enter a number")
For i = 1 To num Step 1
If i = 1 Then
ListBox1.Items.Add(1)
ElseIf i = 2 Then
ListBox1.Items.Add(22)
ElseIf i = 3 Then
ListBox1.Items.Add(333)
ElseIf i = 4 Then
ListBox1.Items.Add(4444)
ElseIf i = 5 Then
ListBox1.Items.Add(55555)
ElseIf i = 6 Then
ListBox1.Items.Add(666666)
ElseIf i = 7 Then
ListBox1.Items.Add(7777777)
ElseIf i = 8 Then
ListBox1.Items.Add(88888888)
ElseIf i = 9 Then
ListBox1.Items.Add(999999999)
ElseIf i = 10 Then
ListBox1.Items.Add(1010101010101010101010)
End If
Next
I'm trying to list all numbers with 3 digits where the individual digits sum to a given number.
So far I can return a list of all numbers using this Visual Basic code:
target = 17
i = 1
j = 1
k = 1
Do While i < 10
Do While j < 10
Do While k < 10
r = i + j + k
If r = target Then
If i <> j And j <> k And k <> i Then
lsNumbers.Add(i & j & k )
End If
End If
k += 1
Loop
If k = 10 Then k = 1
j += 1
Loop
If j = 10 Then j = 1
i += 1
Loop
But I want only unique, non repeating combinations.
For example for the target number 17:
179, 197, 269, 278, 287...
I want to be able to test the current number before I add it to the list, to check if it is a combination of a number already in the list - so 197 would fail because of 179, and 287 would fail because of 278
Observations
Just curious, is excluding the 0 digit on purpose?
To iterate through the possible digits, a well suited instruction pair is FOR NEXT. Definitely simpler than the DO WHILE that you used.
Loop
If k = 10 Then k = 1
Loop
If j = 10 Then j = 1
Upon loop completion, the iterator is sure to contain 10. The IF is redundant.
Solution
In order to check if a number, that obeys the condition, is unique in the sense that it is not composed of the same 3 digits as an already validated number, you could consult a 3-D array. If the new number corresponds to a non-zero element in this array, it means that the new number would be using the same digits as an earlier number. That's reason to reject it.
Next code runs in QBasic. You'll have no trouble rewriting it for Visual BASIC.
DIM r%(1 TO 9, 1 TO 9, 1 TO 9)
FOR i% = 1 TO 9
FOR j% = 1 TO 9
FOR k% = 1 TO 9
r%(i%, j%, k%) = 0
NEXT
NEXT
NEXT
target% = 17
FOR i% = 1 TO 9
FOR j% = 1 TO 9
FOR k% = 1 TO 9
IF i% + j% + k% = target% THEN
IF r%(i%, j%, k%) = 0 THEN
PRINT i% * 100 + j% * 10 + k%; " ";
r%(i%, j%, k%) = 1 ' Could do without this one because of the ascending order
r%(i%, k%, j%) = 1
r%(j%, i%, k%) = 1
r%(j%, k%, i%) = 1
r%(k%, i%, j%) = 1
r%(k%, j%, i%) = 1
END IF
END IF
NEXT
NEXT
NEXT
This is my output of valid numbers:
179 188 269 278 359 368 377 449 458 467 557 566
I am looping through a card number finding all the odd numbers and multiplying them by the card digits. Its kind of hard to explain. I am having trouble multiplying the odd number and the card number. Here's an example my teacher gave me. You multiply card number 1 and and odd number 1 and so forth. I am not getting any errors, it just kind of freezes.
Sums
Card #: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
Multiples 1 2 3 4 5 6 7 8
Evens: 2 4 6 8 0 2 4 6 32 =Sum 1
Odds: 1 6 15 28 45 6 21 40 162 =Sum 2
Sum 3: 194
194 =Sum 3
Step 4: =1+9+4 = 14
= 1 + 4 = 5 = check digit
Public Class Payment
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
Dim Sum1 = 0
Dim Sum2 = 0
Dim Sum3 = 0
Dim ready As Boolean
Dim ccnumb = CardNumber.Text
Format(CardNumber.Text, "################")
Dim exp = Mid(ExpDate.Text, 1, 3)
Dim checkdigit = 0
If FullName.TextLength = 0 Or cardtype.Text.Length = 0 And ccnumb.Length <= 16 Or exp.Length = 2 Then
MessageBox.Show("Please enter all credit card information before proceeding.")
ready = False
Else ready = True
End If
If ready = True Then
For Each num As Char In ccnumb
If CInt(CStr(num)) Mod 2 <> 0 Then
Sum1 += CInt(CStr(num)) * CInt(CStr(num)) Mod 2 <> 0
Else
Sum2 += CInt(CStr(num))
End If
Next
Sum3 = Sum1 + Sum2
Do While Sum3 > 10
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop
Do While exp.Length > 1
checkdigit = Mid(ExpDate.Text, 1, 1) + Mid(ExpDate.Text, 1, 2)
Loop
If Sum3 = checkdigit Then
MessageBox.Show("Congratulations! Your payment was successful.")
CustInv.Show()
Else MessageBox.Show("The checkdigit," & Space(1) & Sum3 & Space(1) & "does not match the month code," & Space(1) & checkdigit & "." & Space(1) & "Please reenter your card information.")
End If
End If
End Sub
"it kind of freezes" is lay speak for "my code enters an infinite loop".
This looks suspicious:
Do While Sum3 > 10
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop
To enter the loop, Sum3 must be greater than 10. For the loop to exit, Sum3 must not be greater than 10, but your code only increments Sum3, so Sum3 can only stay greater than 10.
This means that once entered, this loop is infinite.
If changing outer loop to inside loop doesn't help you
Try this
Do While Sum3 > 10 and j < sum3 ' adding j < sum3 might stop the loop
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop
I have the formula to check 9 integers,
First digit(d1) must be: 1, 2, 5, 6, 8 or 9
Last digit(d9) must be: 0 or 9
9xd1+8xd2+7xd3+6xd4+5xd5+4xd6+3xd7+2xd8+d9 mod 11 = 0
I can "validate" the key, but how can I generate more of this, knowing the conditions for it to be right?
How can I generate 9 different integers from 0 to 9 and check them under this formula?
Thanks for helping!
Generate the first 7 digits randomly, calculating the formula for those digits.
Set the 9th digit's value to 9, and add it to the formula.
Calculate a value for the 8th digit based on the mod of the result of the formula that causes the result of the formula to be mod 11 = 0.
For the exception case where attempting to do this causes mod 11 = 9, set the 9th digit to 0.
Implementation:
Private randGen As New Random()
Function GenNum() As Integer
Dim digits(0 To 8) As Integer
GenNum = 0
Dim checkSum As Integer
digits(0) = randGen.Next(6) + 1
If digits(0) >= 3 Then digits(0) += 2
If digits(0) >= 7 Then digits(0) += 1
checkSum += digits(0) * 9
For d As Integer = 1 To 6
digits(d) = randGen.Next(10)
checkSum += digits(d) * (9 - d)
Next
digits(8) = 9
checkSum += digits(8)
If (checkSum Mod 11) Mod 2 = 1 Then
digits(7) = (11 - (checkSum Mod 11)) \ 2
Else
digits(7) = ((12 - (checkSum Mod 11)) \ 2 + 4) Mod 10
End If
checkSum += digits(7) * 2
If checkSum Mod 11 = 9 Then digits(8) = 0
Dim pow10 As Integer = 1
For d As Integer = 8 To 0 Step -1
GenNum += pow10 * digits(d)
pow10 *= 10
Next
End Function
I can help you to generate integers from 0 to 9.
here is how your form should look like:
and here is the code:
Public Class Form1
Dim NumRandom As Random = New Random
Dim X, Y, Z As Integer
Private Sub GenerateBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBUT.Click
Dim a(9), i, j, RN As Integer
Dim flag As Boolean
flag = False
i = 1
a(j) = 1
Do While i <= 9
Randomize()
RN = CInt(Int(9 * Rnd()) + 1)
For j = 1 To i
If (a(j)) = RN Then
flag = True
Exit For
End If
Next
If flag = True Then
flag = False
Else
a(i) = RN
i = i + 1S
End If
Loop
Label1.Text = a(1)
Label2.Text = a(2)
Label3.Text = a(3)
Label4.Text = a(4)
Label5.Text = a(5)
Label6.Text = a(6)
Label7.Text = a(7)
Label8.Text = a(8)
Label9.Text = a(9)
Z = Label4.Text
Y = Label5.Text
X = Z + Y
X = X - Label3.Text
If X > 1 And X < 10 Then
X = NumRandom.Next(1, 7)
If X = 1 Then
Label1.Text = "0"
ElseIf X = 2 Then
Label2.Text = "0"
ElseIf X = 3 Then
Label3.Text = "0"
ElseIf X = 4 Then
Label4.Text = "0"
ElseIf X = 5 Then
Label5.Text = "0"
ElseIf X = 6 Then
Label6.Text = "0"
ElseIf X = 7 Then
Label7.Text = "0"
End If
End If
End Sub
End Class
Please assist me in defining a modulus function for quarters: QuarterMod()
The function is to represent the quarter that you'll get when you take step a certain number of quarters in time.
Example:
Today, Juli 2013, we are at Q3.
What quarter will we be at a year from now? Well, todays quarter plus a year, i.e. 3 + 4
Ans = QuarterMod(3 + 4)
Ans = 3
More examples:
QuarterMod(-1) = 3
QuarterMod(0) = 4
QuarterMod(1) = 1
QuarterMod(2) = 2
QuarterMod(3) = 3
QuarterMod(4) = 4
QuarterMod(5) = 1
And so on.
You mean like this ? ...
Function QuarterMod(ByVal n As Integer) As Integer
If n = 0 Then
QuarterMod = 0
Exit Function
End If
QuarterMod = n Mod 4
If QuarterMod = 0 Then QuarterMod = 4
If QuarterMod < 0 Then QuarterMod = QuarterMod + 4
End Function
How about this:
Public Function QuarterMod(Qtr As Integer)
Dim i As Integer
i = Qtr Mod 4
If i <= 0 Then
i = i + 4
End If
QuarterMod = i
End Function