How do I size and sapce - sql

def function(foo):
print(foo)
``````python
def function(foo):
print(foo)
``````python
def function(foo):
print(foo)
this is what i needed and i appreciate it guys

For problems like these, try to associate the output with its indices (or in this case, current_row). Also, it seems like it's better to start from 0 for this one.
0: 1
1: 2 * 3
2: 4 * * * 5
3: 6 * * * * * 7
4: 8 * * * * * * * 9
max_row is 5, current_row is from 0 to 4.
There are max_row - current_row - 1 spaces on each row.
There are 2 * current_row - 1 stars on each row.
The numbers on the left is just twice current_row, or 2 * current_row. This is true for all except 0. We could use an if statement for that special case.
The numbers on the right is just left + 1, or 2 * current_row + 1.
space = '\t'
star = '*'
size = int(input("Enter number to make triangle: "))
def printRow(current_row, max_row) :
star_count = 2 * current_row - 1
line = space * (max_row - current_row - 1)
if current_row == 0 :
line += "1"
else :
line += str(2 * current_row) + space
line += (star + space) * star_count
if current_row > 0 :
line += str(2 * current_row + 1)
print(line)
if size <= 0 :
print("The value you entered is too small to display a triangle")
for i in range(0, size) :
printRow(i, size)

First of all you have to learn how to print python pyramid you can learn it from
This link , Then try to understand it after you can understand below code
space = '\t'
star = '*'
size = int(input("Enter number to make triangle: \n"))
def printRow(current_row, max_row) :
rPri = 0
lefNum = 0
star_count = 2*current_row - 3
if current_row == 1:
lefNum = 1
if current_row > 1:
lefNum = (current_row-1)*2
rPri = 1
rigNum = lefNum + 1
line = space * (max_row - current_row) + str(lefNum) + (space + star) * star_count + (space + str(rigNum))*rPri
print(line)
if size<=0 :
print("The value you entered is too small to display a triangle")
for i in range(1, size+1) :
printRow(i, size)

Related

Division by Zero error in calculating series

I am trying to compute a series, and I am running into an issue that I don't know why is occurring.
"RuntimeWarning: divide by zero encountered in double_scalars"
When I checked the code, it didn't seem to have any singularities, so I am confused. Here is the code currently(log stands for natural logarithm)(edit: extending code if that helps):
from numpy import pi, log
#Create functions to calculate the sums
def phi(z: int):
k = 0
phi = 0
#Loop through 1000 times to try to approximate the series value as if it went to infinity
while k <= 100:
phi += ((1/(k+1)) - (1/(k+(2*z))))
k += 1
return phi
def psi(z: int):
psi = 0
k = 1
while k <= 101:
psi += ((log(k))/( k**(2*z)))
k += 1
return psi
def sig(z: int):
sig = 0
k = 1
while k <= 101:
sig += ((log(k))**2)/(k^(2*z))
k += 1
return sig
def beta(z: int):
beta = 0
k = 1
while k <= 101:
beta += (1/(((2*z)+k)^2))
k += 1
return beta
#Create the formula to approximate the value. For higher accuracy, either calculate more derivatives of Bernoulli numbers or increase the boundry of k.
def Bern(z :int):
#Define Euler–Mascheroni constant
c = 0.577215664901532860606512
#Begin computations (only approximation)
B = (pi/6) * (phi(1) - c - 2 * log(2 * pi) - 1) - z * ((pi/6) * ((phi(1)- c - (2 * log(2 * pi)) - 1) * (phi(1) - c) + beta(1) - 2 * psi(1)) - 2 * (psi(1) * (phi(1) - c) + sig(1) + 2 * psi(1) * log(2 * pi)))
#output
return B
A = int(input("Choose any value: "))
print("The answer is", Bern(A + 1))
Any help would be much appreciated.
are you sure you need a ^ bitwise exclusive or operator instead of **? I've tried to run your code with input parameter z = 1. And on a second iteration the result of k^(2*z) was equal to 0, so where is from zero division error come from (2^2*1 = 0).

In VB I want to use Unicode characters

I am having a problem, when I try to enter Japanese characters into the VB console they show up as question marks with no data attached to them. I am writing a program to help me conjugate words and I need to be able to input Japanese characters to do so.
Is there a setting I can change to allow the input and output of these characters?
I have found the the solution!
Add the following code to the top of your code:
Console.InputEncoding = System.Text.Encoding.Unicode
Console.OutputEncoding = System.Text.Encoding.Unicode
This will allow the input of foreign characters but it won't allow you to see the actual characters, (the characters will just have value)
Run the code
At the top left of the console, there is a an icon, click it, and then click properties
Go to the font tab and change the font. So far, the ones that work for me are any that begin with "MS", but "SimSun-ExtB" works too.
Click "Ok" and you're done!
This document has a reference for several unicode ranges.
You can try my method as follows. Please pay attention to RichTextBox1.Text &= (ChrW(i)) which is the most important step.
Public Class Form1
Dim First_Unicode, Last_unicode As Integer
'Hexadecimal to Decimal
Public Function H2D(ByVal Hex As String) As Long
Dim i As Long
Dim b As Long
Hex = UCase(Hex)
For i = 1 To Len(Hex)
Select Case Mid(Hex, Len(Hex) - i + 1, 1)
Case "0" : b = b + 16 ^ (i - 1) * 0
Case "1" : b = b + 16 ^ (i - 1) * 1
Case "2" : b = b + 16 ^ (i - 1) * 2
Case "3" : b = b + 16 ^ (i - 1) * 3
Case "4" : b = b + 16 ^ (i - 1) * 4
Case "5" : b = b + 16 ^ (i - 1) * 5
Case "6" : b = b + 16 ^ (i - 1) * 6
Case "7" : b = b + 16 ^ (i - 1) * 7
Case "8" : b = b + 16 ^ (i - 1) * 8
Case "9" : b = b + 16 ^ (i - 1) * 9
Case "A" : b = b + 16 ^ (i - 1) * 10
Case "B" : b = b + 16 ^ (i - 1) * 11
Case "C" : b = b + 16 ^ (i - 1) * 12
Case "D" : b = b + 16 ^ (i - 1) * 13
Case "E" : b = b + 16 ^ (i - 1) * 14
Case "F" : b = b + 16 ^ (i - 1) * 15
End Select
Next i
H2D = b
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Font = New Font("Cambria", 20, FontStyle.Regular)
First_Unicode = H2D(TextBox1.Text)
Last_unicode = H2D(TextBox2.Text)
'display 20 characters each line
Dim i As Integer
For i = First_Unicode To Last_unicode
RichTextBox1.Text &= (ChrW(i))
If i Mod 20 = 0 Then
RichTextBox1.Text &= vbCrLf
End If
Next
End Sub
End Class

Type Mismatch Array Position Compare Vba

I don't understand why I cannot compare an array in VBA. I created an array that starts 0 1 2 3. I added a comparison due to subscript errors trying to compare 0 to 0 - 1 so it can only start comparisons at 1 and continue. Now I'm receiving a type Mismatch 13 and I can't figure out why the data type is different/not working. I'm guessing i in a for loop is not considered an int or something?
It fails at CoordinatesArray(i) = CoordinatesArray(i-1)
Code:
For i = 0 To NumLines - 1
coordx1 = (vLines(12 * i + 6))
coordy1 = (vLines(12 * i + 7))
coordz1 = (vLines(12 * i + 8))
CoordinatesArray(i) = Array(coordx1, coordy1, coordz1)
If i > 0 Then
If CoordinatesArray(i) = CoordinatesArray(i - 1) Then
coordx1 = (vLines(7))
You will need to compare each value in the jagged array separately:
For i = 0 To NumLines - 1
coordx1 = (vLines(12 * i + 6))
coordy1 = (vLines(12 * i + 7))
coordz1 = (vLines(12 * i + 8))
CoordinatesArray(i) = Array(coordx1, coordy1, coordz1)
If i > 0 Then
If CoordinatesArray(i)(1) = CoordinatesArray(i - 1)(1) And _
CoordinatesArray(i)(2) = CoordinatesArray(i - 1)(2) And _
CoordinatesArray(i)(3) = CoordinatesArray(i - 1)(3) Then
coordx1 = (vLines(7))

For Loop running error

This code doesn't find the correct output
for say n= 1 (although it gives the correct output for say n= 2,3,4..etc.)
if we put n= 1 to find x then the i loop will continue from 1 to 0, hence the first term in x should vanish and leftover should be the second term 5; but it gives 0 ?
Is there any limitation on the input n to run the for loop ?I would appreciate any help.
Function math(n As Integer) As Double
Dim i As Integer
Dim x As Double
For i = 1 To n - 1
x = (n - 1) * 2 + 5
sum = sum + x
Next i
math = sum
End Function
Why not simply:
Function math(n As Integer) As Double
Math = ((n - 1) * 2 + 5) * Abs((n - 1) - (n = 1))
End Function
???
if the answer is correct then Math = (n * 2 + 3) * Abs((n - 1) - (n = 1)) would be easier to understand and make much more sense
In the for loop, if you don't precise the Step, the variable will only increment by 1.
And here, you start at 1 to go to 0, so the loop won't execute, you need to test n to cover both cases :
Function math(n As Integer) As Double
If n < 0 Then Exit Function
Dim i As Integer
Dim x As Double
Dim Summ As Double
Select Case n
Case Is > 1
For i = 1 To n - 1
x = (i - 1) * 2 + 5
Summ = Summ + x
Next i
Case Is = 1
Summ = (n - 1) * 2 + 5
Case Is = 0
Summ = 5
Case Else
MsgBox "This case is not supported", vbInformation + vbOKOnly
Exit Function
End Select
math = Summ
End Function
If n = 1, you end up with For i = 1 To 0 which is incorrect and
should be expressed For i = 1 To 0 STEP -1.
So I suggest you add the STEP BYand make sure it is either 1 to -1 depending on N.

Project Euler #1 - Lasso

I've been working on Project Euler questions as part of learning how to code in Lasso and am wondering if my solution can be improved upon. Here is what I've got below for question #1 in Lasso 8 code, and it returns the correct answer:
var ('total' = 0);
loop(1000-1);
loop_count % 3 == 0 || loop_count % 5 == 0 ? $total += loop_count;
/loop;
output($total);
My question: is there a better or faster way to code this? Thanks!
Actually Chris it looks like my L9 code answer was almost exactly the same. However what I had to do to time is was wrap it in a loop to time it 1000 times.
Lasso 9 can do Microseconds, whereas versions prior can only time in milliseconds.
Below are 3 ways - the first is yours, then my two versions.
define br => '<br>'
local(start_time = micros)
loop(1000)=>{
var ('total' = 0);
loop(1000-1);
loop_count % 3 == 0 || loop_count % 5 == 0 ? $total += loop_count;
/loop;
$total;
}
'Avg (L8 code in 9): '+(micros - #start_time)/1000+' micros'
br
br
local(start_time = micros)
loop(1000)=>{
local(sum = 0)
loop(999)=>{ loop_count % 3 == 0 || loop_count % 5 == 0 ? #sum += loop_count }
#sum
}
'Avg (incremental improvement): '+(micros - #start_time)/1000+' micros'
br
br
local(start_time = micros)
loop(1000)=>{
local(sum = 0)
loop(999)=>{ not (loop_count % 3) || not(loop_count % 5) ? #sum += loop_count }
#sum
}
'Avg using boolean not: '+(micros - #start_time)/1000+' micros'
The output is:
Avg (L8 code in 9): 637 micros
Avg (incremental improvement): 595 micros
Avg using boolean not: 596 micros
Note that I didn't use "output" as it's redundant in many situations in 8 and completely redundant 9 :)
There is a fun story about how Gauss once summed numbers, which involves a strategy which can help to avoid the loop.
local('p' = 3);
local('q' = 5);
local('n' = 1000);
local('x' = integer);
local('before');
local('after');
#before = micros
loop(1000) => {
/* In the tradition of Gauss */
local('n2' = #n - 1)
local('pq' = #p * #q)
local('p2' = #n2 / #p)
local('q2' = #n2 / #q)
local('pq2' = #n2 / #pq)
local('p3' = (#p2 + 1) * (#p2 / 2) + (#p2 % 2 ? #p2 / 2 + 1 | 0))
local('q3' = (#q2 + 1) * (#q2 / 2) + (#q2 % 2 ? #q2 / 2 + 1 | 0))
local('pq3' = (#pq2 + 1) * (#pq2 / 2) + (#pq2 % 2 ? #pq2 / 2 + 1 | 0))
#x = #p * #p3 + #q * #q3 - #pq * #pq3
}
#after = micros
'Answer: ' + #x + '<br/>\n'
'Average time: ' + ((#after - #before) / 1000) + '<br/>\n'
/* Different numbers */
#p = 7
#q = 11
#before = micros
loop(1000) => {
/* In the tradition of Gauss */
local('n2' = #n - 1)
local('pq' = #p * #q)
local('p2' = #n2 / #p)
local('q2' = #n2 / #q)
local('pq2' = #n2 / #pq)
local('p3' = (#p2 + 1) * (#p2 / 2) + (#p2 % 2 ? #p2 / 2 + 1 | 0))
local('q3' = (#q2 + 1) * (#q2 / 2) + (#q2 % 2 ? #q2 / 2 + 1 | 0))
local('pq3' = (#pq2 + 1) * (#pq2 / 2) + (#pq2 % 2 ? #pq2 / 2 + 1 | 0))
#x = #p * #p3 + #q * #q3 - #pq * #pq3
}
#after = micros
'Answer: ' + #x + '<br/>\n'
'Average time: ' + ((#after - #before) / 1000) + '<br/>\n'
The output is:
Answer: 233168<br/>
Average time: 3<br/>
Answer: 110110<br/>
Average time: 2<br/>
Although the first time I ran it, that first average time was 18 instead of 3. Maybe Lasso is doing something smart for subsequent runs, or maybe it was just bad luck.
n = input number
x = (n-1)/3 = count of 3 divisible numbers.*
sum3 = (3*x*(x+1)) / 2 = sum of those numbers.**
y = (n-1)/5 = count of 5 divisible numbers.
sum5 = (5*y*(y+1)) / 2 = sum of those numbers.
half_Ans = sum3 + sum5
but 15, 30, 45... count twice (in both sum3 & sum5).
so remove it one time, so only they count once.
z = (n-1)/15 = count of 15 divisible numbers.
sum15 = (15*z*(z+1)) / 2 = sum of those numbers.
Answer = half_Ans - sum15
* => (n-1)/3 gives count of 3 divisible numbers.
if n = 100 we need to count of (3, 6, 9, ..., 99)
3 is 1st, 6 is 2nd, .... so on 99 is 33rd
so total count of those number is gain by last number / 3
last number is near to our input n (specifically less than input n)
if n = 99 we must not count 99, because statement is "find the sum of all the multiples of 3 or 5 below n".
so w/o subtract 1 last unwanted number also count, if n is divisible by 3.
** => (3*x*(x+1)) / 2 gives sum of those numbers
if n = 100 sum id 3 + 6 + 9 + ... + 99
all component are multiple of 3.
so 3 + 6 + 9 + ... + 99 = 3*(1 + 2 + 3 + ... + 33)
sum of 1 to m is (m*(m+1)) / 2
so 3 + 6 + 9 + ... + 99 = (3*33*(33+1)) / 2
here m for 1 to m is last number or total number of that sequence
or length of sequence that's why we find count of divisible numbers.