with C++ shortest path algorithm with two numbers. (c++) - c++

if I got two integer a, b, I have to get the number of cases 2-D shortest path.
If a = 2, b = 3,
h- - -
| | | |
- - -
| | | |
- - -g
the path is from h to g.
Could you tell me how to get the number of cases with C++?

If you want the number of (shortest) paths, it would be
(a + b)! / (a! * b!)
! denote factorial
(permutation count with repetition).
(10 in your case as 5!/(2!*3!)).
std::next_permutation would be a good tool to iterate over them.

Related

How does recursion get previous values?

I'm in the basic of the basic of learning c++, and ran into an example of recursion that I don't understand. The equation is for Fibonacci numbers, and is shown below:
int fibo(int f)
{
if (f < 3)
{
return 1;
}
else
{
return fibo(f - 2) + fibo(f - 1);
}
}
How does the "else" statement work? I know that it adds the two previous numbers to get the current fibbonacci number, but how does it know, without any prior information, where to start? If I want the 7th fibonacci number, how does it know what the 6th and 5th numbers are?
In this given equation, It will go deeper in the root. When you have given Value 7 initially, it will go to function itself to get value of 7-2 = 5 and 7-1=6, still its has not value of 5 and 6. so further it will decrease value of 5 to 3 and 4 and 6 to 5 and 4.
at the end when f is less then 3 it will return value 1. something like that after getting root values it will sum up those values to get total answer.
A recursive function will call itself as many times as it needs to compute the final value. For example, if you call fibo(3), it will call itself with fibo(2) and fibo(1).
You can understand it better if you write down a tree representing all the function calls (the numbers in brackets are the return values):
fibo(3) [1+1]
|
.--------------.
| |
fibo(2) [1] fibo(1) [1]
For fibo(7), you will have multple calls like so:
fibo(7) [fibo(6) + fibo(5)]
|
.-----------------------------------------------.
| |
fibo(6) [fibo(5) + fibo(4)] fibo(5) [fibo(4) + fibo(3)]
| |
.---------------------------------. ...
| |
fibo(5) [fibo(4) + fibo(3)] fibo(4) [fibo(3) + fibo(2)]
| |
... ...
Each recursive call will execute the same code, but with a different value of f. And each recursive call will have to call their own "editions" of the sub-cases (smaller values). This happens until everyone reaches the base case (f < 3).
I didn't draw the entire tree. But I guess you can see this grows very quick. There's a lot of repetition (fibo(7) calls fibo(6) and fibo(5), then fibo(6) calls fibo(5) again). This is why we usually don't implement Fibonacci recursively, except for studying recursion.

Removing Cyclotomic Factors from a Polynomial - Pari

I want to take some polynomial f and remove all of it's cyclotomic factors, and then look at the resulting polynomial (say g). I'm aware of polcyclofactors and the current code I have tried is:
c(f)=polcyclofactors(f)
p(f)=prod(i=1,#c(f),c(f)[i])
g(f)=f/p(f)
The issue I have is that polcyclofactors doesn't take into account multiplicity of the cyclotomic factors. For example:
f=3*x^4 + 8*x^3 + 6*x^2 - 1
g(f)
= 3*x^3 + 5*x^2 + x - 1
But
factor(f)
=
[ x + 1 3]
[3*x - 1 1]
Is there any way to be able to nicely include multiple cyclotomic factors of f to divide by? Or will I have to look at factorising f and try and removing cyclotomic factors that way?
The following two suggestions are based on repeating the divisions until no more can be done (they are both very similar).
Suggestion 1:
r(f)={my(c); while(c=polcyclofactors(f); #c, f=f/vecprod(c)); f}
Suggestion 2:
r(f)={my(g=vecprod(polcyclofactors(f))); while(poldegree(g), f=f/g; g=gcd(f,g)); f}
Another suggestion without a loop:
r(f)={my(g=vecprod(polcyclofactors(f))); numerator(f/g^(poldegree(f)))}
Now a version that is probably superior: For each factor valuation can be used to get the required power.
r(f)={f/vecprod([t^valuation(f,t) | t<-polcyclofactors(f)])}

Size of propositional-logic formula in Standard ML

I'm working on this problem, where the propositional logic formula is represented by:
datatype fmla =
F_Var of string
| F_Not of fmla
| F_And of fmla * fmla
| F_Or of fmla * fmla
Im trying to write a function that returns the size of a propositional-logic formula. A propositional variable has size 1; logical negation has size 1 plus the size of its sub-formula; logical conjunction and disjunction have size 1 plus the sizes of their sub-formulas.
How would I go about trying to solve this problem?
In general, when you have a sum type like this, it can be a good idea to start with a function definition that just lists each case but leaves out the implementation:
fun size (F_Var v) =
| size (F_Not f) =
| size (F_And (f1, f2)) =
| size (F_Or (f1, f2)) =
and then you fill in the definitions of the cases one at a time as you figure them out.
Since you already have a list of what the size is in each case;
A propositional variable has size 1.
A negation has size 1 plus the size of its sub-formula.
A conjunction has size 1 plus the sum of the sizes of its sub-formulas.
A disjunction has size 1 plus the sum of the sizes of its sub-formulas.
you can pretty much translate it directly into ML:
fun size (F_Var _) = 1
| size (F_Not f) = 1 + size f
| size (F_And (f1, f2)) = ...
| size (F_Or (f1, f2)) = ...
where I have left two cases for you to fill in.
Note that there is a very close correspondence between the definition in English and the definition in ML of each case.

Solve an equation containing three unknown prime numbers [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I was recently asked a question in an interview & have been unable to crack it, after my own efforts have failed & Google not showing any results , I am posting it here so that anyone else may also try their hand on it .
Given the equation:
a (a + b) = c - 120
where a,b & c are unequal prime numbers, find a, b & c.
I know I must use some property of the prime numbers to reduce the problem to a simpler one, but I can't think of one. Any suggestions/solutions will be appreciated.
The best I could come up with is that :
There may be multiple solutions to it. My first approach was a brute force search for 3 prime numbers that solved this equations. (I know , totally useless)
The second approach was a refinement of the first, to modify the equation to a (a + b) - 120 = c. So now we reduce our brute force variables to just a & b & check if the LHS is prime no for the selected a & b. (If c were to be large, finding out whether the LHS is a prime would take away the advantage gained by reducing the variables from 3 to 2.)
So you see, I am not really going anywhere.
all primes are odd, except 2 - (1)
all primes are positive - (2)
odd - even = odd (3)
(1), (2) => c > 120 and c is odd - (4)
odd * odd = odd - (5)
(3), (4), (5) => c-120 is odd => a(a+b) is odd - (6)
even + odd = odd - (7)
(6) => a is odd, a+b is odd (8)
(7), (8) => b is even => b = 2
So, we have a^2 + 2a = c-120
I couldn't go any further
Let's stipulate that c > 120. That implies c != 2. So the RHS is odd.
Therefore the LHS has to be odd, so a (a + b) has to be odd. So a is odd, and a+b is odd. This only works out if b is even, and b is prime, so b = 2.
So we have a(a+2) = c - 120.
So a^2 + 2a + (120-c) = 0
Using the quadratic formula, solving for a, we get
[-2 +- sqrt(2^2 - 4 * 1 * (120 - c))] / 2
= -1 +- sqrt(1 - (120-c))
= -1 + sqrt(c - 119)
So we need a prime number c, so that c - 119 is a perfect square.
This is a quick calculation with a table of primes.
The smallest one I can find is c = 263, so a = 11, b = 2
It looks like c=443, a=17, b=2 also works.
There don't appear to be any other c values below 1000.
Possibly there are many, many others.

looping for a series

I have this question:
Write a program to display the sum of the series 1+1/2+2/3+3/4+...
+(n-1)/n (using for loop).
I did not understand the series well, kindly explaint it for me if n = 6. (no need for coding).
For n = 6, you need to calculate 1 + (1/2) + (2/3) + (3/4) + (4/5) + (5/6)
The question is asking you to fill the details in to the following program:
sum = 0;
for (int i=1; i<=n; ++i) {
sum += ???
}
return sum;
where ??? should give you the following values:
i | ???
-------
1 | 1
2 | 1/2
3 | 2/3
4 | 3/4
5 | 4/5
6 | 5/6
.
.
.
n | (n-1)/n
It is simple. The biggest hint is the nth term itself : (n-1)/n
Except the first term, every other term can be represented by an expression of the form of (i-1)/i, which means the algorithm boils down to this:
double sum = 1.0; //first term
for(int i = 2 ; i <= n ; ++i) //2nd to nth term!
sum += (i-1.0)/i;
Why did I write (i-1.0) instead of (i-1)?
You need to figure that out yourself, as I already have explained and written almost the whole code.
Write a loop that evaluates (n-1)/n for each value of n and adds the outcome to some variable.
That "some variable" is the answer.
Set n=6
The final term of the series can also be written as n / (n + 1) where n is a value that iterates.