Given an integer n <= 10^18 which is the product of Fibonacci numbers, I need to factor it into said Fibonacci numbers.
Each factorization has a score, which is one less than the count of factors plus the sum of the indices of the factors in the Fibonacci sequence that begins with f(1) = 1, f(2) = 2.
If multiple such factorizations are possible, I need the factorization that minimizes the score.
Example:
104 = 13 * 8 or 104 = 13 * 2 * 2 * 2
f(6) = 13, f(5) = 8, f(2) = 2
For 104 = 13*8 = f(6)*f(5), we have a count of 2, indices of 6 & 5, giving us 2 + 6 + 5 - 1 = 12.
For 104 = 13 * 2 * 2 * 2 = f(6) * f(2) * f(2) * f(2), we have a count of 4 and indices of 6, 2, 2, 2, giving us 4 + 6 + 2 + 2 + 2 - 1 = 15.
We should pick 13 * 8 since it has the lower score.
The biggest problem I've come across is when we have a number like 1008, which is divisible by 144 and 21, but needs to be divided by 21 because 1008 % 7 == 0. Because my program is first dividing by the biggest numbers, number 144 is 'stealing' 3 from number 21 so my program doesn't find a solution.
Carmichael's theorem proves that each Fibonacci number after 144 has at least one prime divisor that doesn't divide any earlier Fibonacci number.
There aren't many Fibonacci numbers under 10^18; fewer than 90.
Make an array of all the Fibonacci numbers <= 10^18.
Given an input n which is the product of Fibonacci numbers, its factorization into Fibonacci numbers must include every Fibonacci number above 144 that divides it, repeated as many times as it divides it.
Go through your Fibonacci numbers in descending order and keep dividing n by any such number that divides it, until you get to 144.
Now we need to be careful because two Fibonacci numbers don't have any prime factors not seen in previous Fibonacci numbers. These are 8 and 144. Since 8 is 2^3 and 2 is a Fibonacci number, you can't render your number unfactorable into Fibonacci numbers by taking the 8. Under your optimization, you will always choose the 8.
Then 144 is the only factor that you might need to reject for a smaller factor. This can only happen if 34 or 21 are factors, and the 144 eliminates a needed 2 or 3.
34 = 2 * 17, 21 = 3 * 7
That was long-winded, but it gets us to a simple approach.
Go through the Fibonacci numbers <= n in descending order until you get to 144, then skip to 34, then 21, then back to 144 and descending down to 2.
This will give you the optimal factorization under your weird scoring scheme.
----- this order -----
[679891637638612258, 420196140727489673, 259695496911122585, 160500643816367088, 99194853094755497, 61305790721611591, 37889062373143906, 23416728348467685, 14472334024676221, 8944394323791464, 5527939700884757, 3416454622906707, 2111485077978050, 1304969544928657, 806515533049393, 498454011879264, 308061521170129, 190392490709135, 117669030460994, 72723460248141, 44945570212853, 27777890035288, 17167680177565, 10610209857723, 6557470319842, 4052739537881, 2504730781961, 1548008755920, 956722026041, 591286729879, 365435296162, 225851433717, 139583862445, 86267571272, 53316291173, 32951280099, 20365011074, 12586269025, 7778742049, 4807526976, 2971215073, 1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986, 39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229, 317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610, 377, 233, 34, 21, 144, 89, 55, 13, 8, 5, 3, 2]
Related
Design the source code to print the Knight sequence of a given number n.
Knight Sequence of a number starts with itself, the remaining terms of the sequence are
the sum of proper divisors of the immediate previous term.
Examples:
The knight sequence for 10 is 10, 8, 7, 1, 0.
(Sum of proper divisors of 10 is 5 + 2 + 1 = 8.
Sum of proper divisors of 8 is 4 + 2 + 1 = 7.
The Sum of proper divisors of 7 is 1.
The Sum of proper divisors of 1 is 0.)
Note that there is no proper divisor of 1.
The knight sequence for 6 is 6 (an infinite sequence of 6s).
When the knight sequence is repeating, print the repeating number and stop.
I am trying to solve this question and wrote this code:
n = int(input('Enter the value: '))
l = [n]
s = 0
for i in range(1, n):
if n % i == 0:
s += i
l.append(s)
print(set(l))
and this gives the output as:
Enter the value: 10
{8, 10}
The issue is that I am not able to figure out that how to continue this iteration to find the rest of the sequence....pls help me out
I have 3 valid values: a,b,c
I can insert them in a Nx3 grid in such a way that no row or column contains cells that are the same values. How can I calculate the number of valid patterns that can be created using N rows?
For example, if N = 4, the total number of valid pattern is 296490.
This is my attempt:
def countPatterns(n):
dp1, dp2 = 6,6
mod = 10 ** 9 + 7
for i in range(2, n+1):
dp1, dp2 = (dp1 * 3 + dp2 * 2) % mod, (dp1 * 2 + dp2 * 2) % mod
return (dp1 + dp2) % mod
When N = 4, the output should be 174 but I'm getting 54.
This can be solved using standard techniques from combinatorics.
import math
# Counts the number of grids where the first i rows and first j columns are
# monochrome. All of the monochrome rows and columns must have the same color if
# and only if both i and j are nonzero. Otherwise, the color choices are
# independent.
def count_monochrome(n, i, m, j):
return 3 ** (1 if i and j else i + j) * 3 ** ((n - i) * (m - j))
# Uses inclusion-exclusion to count the number of grids with no monochrome row
# or column.
def count(n, m=3):
# There are math.comb(n, i) * math.comb(m, j) intersections with i
# monochrome rows and j monochrome columns.
return sum(
math.comb(n, i)
* math.comb(m, j)
* (-1) ** (i + j)
* count_monochrome(n, i, m, j)
for i in range(n + 1)
for j in range(m + 1)
) % (10 ** 9 + 7)
print(count(4))
Short Answer
This can also be solved mathematically and so the formula is:
24n - (9 * 8n - 9 * 2n - 18 * 3n + 24)
def countPatterns(n):
result = 24**n - (9 * 8**n - 9*2**n - 18*3**n + 24)
mod = 10 ** 9 + 7
return result % mod
print (countPatterns(4))
296490
Long Answer
1. Condition : no row contains same values
For a given row, there are 3 ways to set a colour in each cell. So, there are 33=27 possible combinations. However, out of these 27 combinations, 3 are GGG, BBB and RRR. So, they have to be excluded and the number of valid combinations in a row is 27 - 3 = 24.
Hence, the total number of combinations for a table with n rows is 24n
2. Condition : no column contains same values
To solve this case, we can count all "invalid combinations" and then subtract their number from the total number (which is 24n as stated in the previous section).
2.1 Invalid combinations
Only first column has same values.
Only second column has same values.
Only third column has same values.
Only 2 first columns have same values (value from the first column is not necessarily equal to the other one).
Only 2 last columns have same values.
Only first and last columns have same values.
Values within each column are equal.
Obviously, number of combinations for 1 is equal to 2 and 3. The same can be noted about 4, 5 and 6.
If we sum up combinations from 1 to 7, then we'll get the total number.
However, it's hard to calculate (1)-(7) individually, but easier to calculate the number of combinations such that at least k columns have the same values. Once those are found, we will apply an inclusion-exclusion principle to find the number of combinations asked in the question.
2.1.1 First column has same values
For each row, there are 8 combinations such that the row starts with R:
RRG
RRB
RGR
RGG
RGB
RBR
RBG
RBB
That means that for a table with n, there are 8n combinations in which first column consists of 8s. Thereof, there are 3*8n combinations such that the first column starts from R, G or B.
Now, it's important to realise that this number represents 4 invalid cases - 1, 4, 6 and 7 (all sets of columns containing the first column).
2.1.2 Second column has same values
Basically, we can use the same logic as above to realise that it's also 3*8n.
This represents 2, 4, 5 and 7.
2.1.3 Third column has same values
Same as before - 3*8n.
This covers 3, 5, 6 and 7.
If we add up combinations from these 3 steps, we will get 9*8n. However, as noted in those steps, we will count 4, 5 and 6 twice. 7 will be counted thrice.
2.1.4 2 columns have same values
Let's count it for 2 first columns because the number for every pair of columns is still the same.
If a row starts with 2 identical colours, then there are 2 combinations for each colour. If not, then there are 3 combinations for each pair (there are 6 possible pairs). Hence, the total number of combinations is 3 * 2n + 6 * 3n.
So, the number of combinations for 4 and 7 is 3 * 2n + 6 * 3n.
The number of combinations for 5 and 7 is 3 * 2n + 6 * 3n.
The number of combinations for 6 and 7 is 3 * 2n + 6 * 3n.
2.1.5 Values within each column equal
This means that all rows must be equal. As there are only 24 ways to construct a row, there are 24 ways to have all values equal within columns. So, it's 24.
Total number of invalid cases
As said before, if we sum up 2.1.1, 2.1.2 and 2.1.3, then we will count 4, 5 and 6 twice, 7 - 3 times (from 2.1). Since the number of combinations for each pair of columns is the same, we can just subtract 3 * 2n + 6 * 2n 3 times (from 2.1.4), and then add the number of combinations for a three column case once (because it won't be there after the subtraction).
9 * 8n - 3 * (3 * 2n + 6 * 3n) + 24 = 9 * 8n - 9 * 2n - 18 * 3n + 24
3. Final Answer
Now, just subtract the number of invalid cases from 24n:
24n - (9 * 8n - 9 * 2n - 18 * 3n + 24)
Don't forget to apply a modulo operation by 109 + 7 at the end.
I have been working on dealing with n! <= 10^6 in python by using sympy.solver. For example, the below is my code:
import sympy as sy
print(sy.solve_univariate_inequality(sy.factorial(n) <= 10**6,n))
I tried solve_univariate_inequality and solve methods but none of them worked. The error was "raise NotImplementedError('solveset is unable to solve this equation.')"
I'm curious if there's any other way to deal with this inequality. Any insight please?
i = 1
factorial = 1
while factorial <= 1_000_000:
factorial *= i
i += 1
print(f"equation valid for n in [1-{i-2}]")
From docs
Currently supported:
polynomial
transcendental
piecewise combinations of the above
systems of linear and polynomial equations
systems containing relational expressions
Looks like factorial is not supported.
If you approach this problem purely mathematically you can just increment n to get the factorial which exceeds the threshold.
results=[]
n=0
n_factorial=1
while n_factorial<=10**6:
results.append(n)
print(n,n_factorial)
n+=1
n_factorial*=n
print(results)
Output:
0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I want to know if there is a math expression that I can use to find this relation between two numbers.
Some examples of the input and expected output are below:
Input Multiple Result
4 3 3
6 3 6
8 3 6
4 4 4
12 4 12
16 5 15
Also, the expressions below from Wolfram Alpha show me the expected result but since they don't expand on the explanation on how to do it I can't learn from them...
Biggest multiple of 4 from 10
Biggest multiple of 4 from 12
try with // and % operators!
for //, you would do
Result = (Input // Multiple) * Multiple
This way you get how many times Multiple Fits into Input - this number is then multiplied with the Multiple itself and therefore gives you the expected results!
EDIT: how to do it with modulo %?
Result = Input - (Input % Multiple)
taken from MCO's answer!
You can employ modulo for this. For example, to calculate the biggest multiple of 4 that is less or equal than 13:
13 % 4 = 1
13 - 1 = 12
in python, that could look like this:
def biggest_multiple(multiple_of, input_number):
return input_number - input_number % multiple_of
So you use it as:
$ biggest_multiple(4, 9)
8
$ biggest_multiple(4, 12)
12
Here's how I would do it:
return int(input / multiple) * multiple
It truncates the division so that you get an integer, which you can multiply.
This can be trivial but damn easy to understand. To take into account if multiple is negative or zero
Multiple=[3,3,3,4,4,5,0,-5]
Input=[4,6,8,4,12,16,1,8]
Result=[]
for input,multiple in zip(Input,Multiple):
if(multiple):
Result.append((range(multiple,input+1,abs(multiple)))[-1])
else:
Result.append(0)
print(Result)
Output:
[3, 6, 6, 4, 12, 15, 0, 5]
As you can see, dividing 3/7 yields a fraction. But when I do 3%7 it yields 3. How could this be? I suppose I expected an output value of 4 (because it would take 4 to complete 7) or 0, (because there is no remainder at all if you use integer division such as 3//7).
>>> 3/7
0.42857142857142855
>>> 3%7
3
>>>
Just trying to understand the depths of Python. Thanks!
Remember long division? Before you learned about fractions, 50 divided by 7 would be 7, remainder 1. The remainder is the modulus. It is the numerator of the 1/7 remaining after integer division.
Let's use different numbers for demonstration.
42 divided by 5 gives a quotient of 8 and a remainder of 2. That means 42 // 5 == 8 and 42 % 5 == 2.
3 divided by 7 gives a quotient of 0 and a remainder of 3. That means 3 // 7 == 0 and 3 % 7 == 3.
In Python, // and % represent the quotient and remainder you probably learned about before you learned about fractions and real numbers. The only (possible) difference is that // floors and % matches the sign of the right-hand operand.
modulo returns the whole number after integer (floor) division.
>>>3//7
0 # with remainder 3
>>>3%7
3
>>>2//5
2 # with remainder 1
>>>2%5
1
Re-reading your question, it occurred to me you got your terms mixed up and that may have been the underlying confusion that none of us properly answered.
But when I do 3%7 it yields 3. How could this be? I suppose I expected an output value of 4 (because it would take 4 to complete 7)
So first that issue was masked when you said 4. Since 4 is greater than 3, 3 can be subtracted a second time, leaving 1. So 1 is the output of 7 % 3.
But you asked about 3 % 7, even though you then proceeded to explain 7 % 3. 3 % 7 is less than 1 (because 7 > 3). So that is why the modulus is still 3. Integer division gives you 0, so 3 is left.
Take the first term (3) divided by the second term (7) using integer division (resulting in 0). Subtract that number from the first term (3) and you get 3. So: 3 % 7 = 3