Product of n numbers with sums in pseint - sum

The product with sums in pseint:
They ask me to get the product of a number n times another but with sum in pseint. I did it but I have to represent the solution like for example: In this case 2 and 3 would be 2+2+2=6, I just don't know how to write that 2+2+2, instead of "The result is ", m, "added", n "times =", prod.
Process Sum_Product
Define i, prod, n, m as Integer;
Write " Type a number ";
Read m;
Write "Type the amount of times u want to sum it ";
Read n;
prod<-0;
For i<-1 to n with Step 1 do
prod<-prod +m;
end of for;
Write "The result is ", m, "added", n "times =", prod;

Related

McDonald's sells Chicken McNuggets only in packages of 6, 9 or 20. What is the largest number of McNuggets that cannot be bought exactly?

Question is from MIT OCW Course Number 6.00, As Taught in Fall 2008:
Here is a theorem below:
If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for some x, then it is possible to buy any number of McNuggets >= x, given that McNuggets come in 6, 9 and 20 packs.
Using the theorem above, write an exhaustive search to find the largest number of McNuggets that cannot be bought in exact quantity, i.e. write an iterative program that finds the largest number of McNuggets that cannot be bought in exact quantity. Format of search should follow the outline below:
Hypothesise possible instances of numbers of McNuggets that cannot be purchased exactly, starting with 1.
For each possible instance, called n, test if there exists non-negative integers a, b, and c, such that 6a+9b+20c = n.
If n McNuggets cannot be bought in exact quantity, save n.
When have found 6 consecutive values of n where 6a+9b+20c = n, the last answer that was saved (not the last value of n that had a solution) is the correct answer, since from the theorem, any amount larger than this saved value of n can also be bought in exact quantity
The error is in line 14 of the code below and this is the error:
elif(6*a + 9*b + 20*c < n_saved or 6*a + 9*b + 20*c > n_saved):
^
SyntaxError: invalid syntax
Here is the code:
def largest_not(a, b, c, n, n_saved):
a = 0
b = 0
c = 0
n = 0
n_saved = 0
for a in range (10):
for b in range (10):
for c in range (10):
for n in range (10):
for n_saved in range (10):
if (6*a + 9*b + 20*c == n):
print (n)
elif(6*a + 9*b + 20*c < n_saved or 6*a + 9*b + 20*c > n_saved):
print (n_saved)
if (n - n_saved > 5):
print "Largest number of McNuggets that cannot be bought in exact quantity: " + "<" + n_saved + ">"
else :
print "Have not found the largest number of McNuggets that cannot be bought in exact quantity."
a=6
b=9
c=20
largest_not(a, b, c, n, n_saved)
Here is a way to solve this:
def check(n):
"""check if n nuggets can be bought exactly with packs of 6, 9 and 20"""
for a in range(20):
for b in range(20):
for c in range(20):
if (6*a+9*b+20*c==n):
return True
return False
### look for a serie of 6 successives n
### to apply the theorem
nb_i = 0 ## number of successive good n found
sv_i = 0 ## last good n found
bad_i = 0 ## last bad n found
for i in range(1, 100):
if (check(i)):
nb_i+=1
sv_i=i
else:
bad_i=i
nb_i=0
sv_i=0
if nb_i==6:
print "Solved: the biggest number of nuggets you cannot buy exactly is: " + bad_i
break
result is:
Solved: the biggest number of nuggets you cannot buy exactly is: 43
Your elif needs to be inline with your if. However, your algorithm will also not work.
I got 43 as the largest number not possible: I'm sure this solution could be optimised.
def check(n):
# a in [0..max times 6 goes into n]
for a in range(0, n // 6 + 1):
# b in [0..max times 9 goes into remainder]
for b in range((n - 6*a) // 9 + 1):
# c in [0..max times 20 goes into remainder]
for c in range(0, (n - 6*a - 9*b) // 20 + 1):
if 6*a + 9*b + 20*c == n:
return (a, b, c)
return None
def largest_not():
n = 1
last_n_not_possible = 1
while (n - last_n_not_possible <= 6):
can_purchase = check(n)
if can_purchase is not None:
print("Can purchase ", n, ' = ', can_purchase[0],'*6 + ', can_purchase[1],'*9 + ', can_purchase[2], '*20', sep='')
else:
print("Can't purchase", n)
last_n_not_possible = n
n = n + 1
return last_n_not_possible
print("Answer: ", largest_not())

Using factorials to find combinations

So, this is a two part question but based on the same project. I am trying to write a small program that can illustrate how a computer can quickly crack a password, using a brute force attack. It only has three inputs: A check box to denote if it should use integers, a check box to denote if it should use letters, and a textbox to enter the password to be cracked. It then outputs the number of combinations. Here is my code:
dim a,b,c,d,P as double
'Using the following formula:
'P(n,r) = n!/(r!(n-r)!)
'Let's assume we are just using numbers, so n = 10
'r = the count of characters in the textbox.
a = factorial(n)
b = factorial(r)
c = (n - r)
d = factorial(c)
P = a / (b * d)
Output = "With a password of " & r & " characters and " & n & " possible values, the number of combinations are " & P
Me.RichTextBox1.Text = Output & vbCrLf
Function factorial(ByVal n As Integer) As Integer
If n <= 1 Then
Return 1
Else
Return factorial(n - 1) * n
End If
End Function
So, let's assume I'm only looking at the characters 0-9, with the following number of characters in a password, I get:
P(10,1) = 10!/(1! * (10-1)!) = 10
P(10,2) = 10!/(2! * (10-2)!) = 45
P(10,3) = 10!/(3! * (10-3)!) = 120
P(10,4) = 10!/(4! * (10-4)!) = 210
P(10,5) = 10!/(5! * (10-5)!) = 252
P(10,6) = 10!/(6! * (10-6)!) = 210
P(10,7) = 10!/(6! * (10-7)!) = 120
You can see the number of combinations goes down, once it gets past 5. I assume this is right, but wanted to check before I present this. Is this because the total number in the pool remains the same, while the sample increases?
My second question is about how to consider a password to crack that repeats numbers. Again, let's assume that we are just pulling from digits 0-9. If the sample size it two (lets say 15), then there are 45 possible combinations, right? But, what if they put in 55? Are there still 45 combinations? I suppose the computer still needs to iterate over every possible combination, so it would still be considered 45 possibilities?

I'm trying to multiply the number of input statements by the number entered in a prior input statement

num1=input("How many days of weather do you have?")
num1=int(num1)
j = input("What is the rainfall for day1? \n")
b = num1
print (j * b)
When the user inputs the days of weather they have, I want it to produce that number of input statements, and when they put in their inputs I'll use those numbers to get the total. My problem is when I run this it just multiplies whatever the j input is by num1, I want it to multiply the number of input statements by num1.
I think you are trying to fund the sum of the total number of days? In that case you need to add each entry.
num1=input("How many days of weather do you have?")
num1=int(num1)
total=0
for num in range(1,num1+1):
j = input("What is the rainfall for day{}?".format(num))
total+=int(j)
print (total)

Calculating the time complexity

Can somebody help with the time complexity of the following code:
for(i = 0; i <= n; i++)
{
for(j = 0; j <= i; j++)
{
for(k = 2; k <= n; k = k^2)
print("")
}
a/c to me the first loop will run n times,2nd will run for(1+2+3...n) times and third for loglogn times..
but i m not sure about the answer.
We start from the inside and work out. Consider the innermost loop:
for(k = 2; k <= n; k = k^2)
print("")
How many iterations of print("") are executed? First note that n is constant. What sequence of values does k assume?
iter | k
--------
1 | 2
2 | 4
3 | 16
4 | 256
We might find a formula for this in several ways. I used guess and prove to get iter = log(log(k)) + 1. Since the loop won't execute the next iteration if the value is already bigger than n, the total number of iterations executed for n is floor(log(log(n)) + 1). We can check this with a couple of values to make sure we got this right. For n = 2, we get one iteration which is correct. For n = 5, we get two. And so on.
The next level does i + 1 iterations, where i varies from 0 to n. We must therefore compute the sum 1, 2, ..., n + 1 and that will give us the total number of iterations of the outermost and middle loop: this sum is (n + 1)(n + 2) / 2 We must multiply this by the cost of the inner loop to get the answer, (n + 1)(n + 2)(log(log(n)) + 1) / 2 to get the total cost of the snippet. The fastest-growing term in the expansion is n^2 log(log(n)) and so that is what would typically be given as asymptotic complexity.

AMPL: Subscript out of bound

Hello fellow optimizers!
I'm having some issues with the following constraint:
#The supply at node i equals what was present at the last time period + any new supply and subtracted by what has been extracted from the node.
subject to Constraint1 {i in I, t in T, c in C}:
l[i,t-1,c] + splus[i,t] - sum{j in J, v in V, a in A} x[i,j,v,t,c,a]= l[i,t,c];
which naturally causes this constraint to provide errors the first time it loops, as the t-1 is not defined (for me, l[i,0,c] is not defined. Where
var l{I,T,C} >= 0; # Supply at supply node I in time period T for company C.
param splus{I,T}; # Additional supply at i.
var x{N,N,V,T,C,A} integer >= 0; #Flow from some origin within N to a destination within N using vehicle V, in time T, for company C and product A.
and set T; (in the .mod) is a set defined as:
set T := 1 2 3 4 5 6 7; in the .dat file
I've tried to do:
subject to Constraint1 {i in I, t in T: t >= 2, c in C}:
all else same
which got me a syntax error. I've also tried to include "let l[1,0,1] := 0" for all possible combinations, which got me the error
error processing var l[...]:
no data for set I
I've also tried
subject to Constraint1 {i in I, t in T, p in TT: p>t, c in C}:
l[i,t,c] + splus[i,p] - sum{j in J, v in V, a in A} x[i,j,v,p,c,a]= l[i,p,c];
where
set TT := 2 3 4 5 6;
in the .dat file (and merely set TT; in the .mod) which also gave errors. Does someone have any idea of how to do this?
One way to fix this is to specify the condition t >= 2 at the end of the indexing expression:
subject to Constraint1 {i in I, t in T, c in C: t >= 2}:
...
See also Section A.3 Indexing expressions and subscripts for more details on syntax of indexing expressions.