How do I convert this to precise columns and rows? - row

having a hard time converting to neat columns and rows. Can anyone help?
I have been trying to set columns and rows to 0 and work from there but I cant figure it out.
I just dont know where to insert the information.
n = 5
rate = 0.05
for n in range(0, 3):
principal = 10000
for n in range (0,6):
principal <= 15000
simple = principal * (1 + rate * n)
compound = principal * (1 + rate)**n
ratea = rate * 100
ratea = int(ratea)
principal = int(principal)
print((ratea),"% $",principal," $","{:.2f}".format(simple)," $","{:.2f}".format(compound))
principal = principal + 1000
rate = rate + 0.05

Use the string method format.

Related

Full recovery data using reed solomon

I'm testing a Reed Solomon algorithm from this repository in order to recover info in case something is externally changed.
Assuming:
m = bits per symbol
k = data
r = redundance
n = bits per block = r + k = 2^m - 1
t = error correction = (n - k) / 2
I can codify and recover info using the following parameters:
m = 8
n = 255
r = 135
k = 120
t = 67
And works perfectly, I can recover 67 errors.
My assumtions are:
Only data will be corrupted, no redundancy.
To get full recovery n = 3 * k --> r = 2 * k.
Then n = 255 so r in this case shall be 170.
So I must have GF(2^m) and RS [n, k, n - k + 1] for use is GF(2^8) and RS [255, 85, 171]
With these parameters I get the error:
Error - Failed to create sequential root generator!
This means that library function make_sequential_root_generator_polynomial:
const std::size_t field_descriptor = 8; /* m = bit per symbol */
const std::size_t generator_polynomial_index = 120;
const std::size_t generator_polynomial_root_count = 170; /* root shall be equal to redundance */
const schifra::galois::field field(field_descriptor,
schifra::galois::primitive_polynomial_size06,
schifra::galois::primitive_polynomial06);
if (
!schifra::make_sequential_root_generator_polynomial(field,
generator_polynomial_index,
generator_polynomial_root_count,
generator_polynomial)
)
{
std::cout << "Error - Failed to create sequential root generator!" << std::endl;
return false;
}
My problem is that I don't know why the algorithm fails. I think I have a concept problem, bug after read about the topic here and here, I don't get why is not possible.
Is it possible to use according assumtions or theory say that this is not possible?
The current code in the question is failing because it sets
generator_polynomial_index = 120;
and 120 (index) + 170 (root count) is > 255 (field size), which is checked for in
make_sequential_root_generator_polynomial()
generator_polynomial_index is normally set to 0 (first consecutive root = 1) or 1 (first consecutive root = field primitive = 2), unless the goal is to use a self-reciprocal generator polynomial.
Even in the case of a self-reciprocal poly, for 170 roots, generator_polynomial_index = 128 - (170/2) = 43. Setting it to 120 is unusually high.
It could be possible for this to work as the roots are sequential powers modulo 255, so they could just wrap around, 2^120, 2^121, ... , 2^253, 2^254, 2^255=2^0, 2^256 = 2^1, ..., as this is done for self-reciprocal polynomials for an odd number of roots generator_polynomial_index = (255 - (number of roots / 2)), but perhaps the rest of the code has an issue with this.

Unit testing Datetime values

I have a function that makes use of the current time (now). The Contract as a whole is a Crowdfunding token and the cost of tokens differ depending on the date and time that tokens are purchased.
How does one simulate different times when testing a Smart Contract? For instance, with regards to the code below, I would like to do unit testing to find out if the code for setting price is correct but I can't change the value of now.
Would it be a good solution to simply substitute the now keyword for another temporary testing variable, say now_sim and then manually changing now_sim during simulation?
if (now < (startTime + 1 days)) {
currentPrice = safeDiv(safeMul(price, 80), 100); // 20 % discount (x * 80 / 100)
}
else if (now < (startTime + 2 days)) {
currentPrice = safeDiv(safeMul(price, 90), 100); // 10 % discount (x * 90 / 100)
}
else if (now < (startTime + 12 days)) {
// 1 % reduction in the discounted rate from day 2 until day 12 (sliding scale per second)
// 8640000 is 60 x 60 x 24 x 100 (100 for 1%) (60 x 60 x 24 for seconds per day)
currentPrice = price - safeDiv(safeMul((startTime + 12 days) - now), price), 8640000);
}
else {
currentPrice = price;
}
If you use pyethereum for testing - which I highly recommend, it's lovely - you can directly alter the timestamp of the simulated block that is mining your transaction.
self.s = t.state()
self.s.block.timestamp = self.s.block.timestamp + 86400
self.s.mine(1)
some_val = your_contract.do_something(some_parameter)
self.assertEqual(some_val, whatever)
See a working example here (maybe a bit out-of-date): https://github.com/realitykeys/subjectivocracy/blob/master/contracts/test.py#L85

picture format negative numbers

I try to format the number into billions, millions and thousands as below
proc format;
picture bmk_fmt (round)
low - 0 = '000,000,000,000)' (prefix='($')
0 - 1e3 = '000,000,000,000' (prefix='$')
1e3 - 1e6 = '000,000,000,009K' (mult=1e-3 prefix='$')
1e6 - 1e9 = '000,000,000,009.9M' (mult=1e-5 prefix='$')
1e9 - high = '000,000,000,009.9B' (mult=1e-8 prefix='$');
run;
However, how can I extend such setting to negative numbers?
E.g (1.2M) & (353K)
You provide almost all information to solve the problem.
I've just have to use basic math.
Using a < sign you can exclude a value from a range e.g. 0 from a negative range.
Here is a final solution.
proc format;
picture bmk_fmt (round default=9)
low - -1e9 = '009.9B)' (mult=1e-8 prefix='($')
-1e9 <- -1e6 = '009.9M)' (mult=1e-5 prefix='($')
-1e6 <- -1e3 = '009.9K)' (mult=1e-2 prefix='($')
-1e3 <-< 0 = '009.9)' (prefix='($')
0 -< 1e3 = '009.9' (prefix='$')
1e3 -< 1e6 = '009.9K' (mult=1e-2 prefix='$')
1e6 -< 1e9 = '009.9M' (mult=1e-5 prefix='$')
1e9 - high = '009.9B' (mult=1e-8 prefix='$');
run;

Calculate modulus for large numbers

Hi I need to calculate (2^n + (-1)^n) % 10000007
where 1 < n < 10^9
How should I go about writing a program for it in c++?
I know this mod property
(a + b)%n = (a%n + b%n)%n but this wont help me.
Given
(a + b)%m = (a%m + b%m)%m
Then, replace both a and b with the same power of 2, and you get the recurrence:
2k+1%m = (2k%m + 2k%m)%m
You probably already figured your formula allows you to break down your problem into:
(2n + (-1)n)%P = (2n%P + (-1)n%P)%P
Then, note that (-1)k is either 1 or -1, and you should be able to calculate your problem in O(n) time.

Calculating proportion with negative float values

I'd like to know if there's any way in C++ to calculate a proportion involving possibily negative values in both vars and extremes.
My goal is to sync a float text input widget with fixed extremes ( eg the user can input any double value between A (min) and B (max) with A,B=any_constant_real_number ) with a slider who can only slide between 0 and 100 ( to simplify ).
If A and B are positive everything is trivial. as
val_slider = ((val_textin-A)*100)/(B-A)
but as A and B can be assumed real it looks to me the only possibility is to use several if/cases, or huge formulas involving a lot of abs() and checks over 0-divisions, whose are quite error prone and very cost intense compared to such an easy task.
Is there any faster and shorter way to achieve the same in c/c++/stl?
Pardon my bad english. Any hint? Thank you.
Your formula should work fine with negative values of A and B as well, as log as A < B.
Example, if you want the user to be able to enter values from -100 to 100, and map these to a slider which goes from 0 - 100, when the user enters -90 you get:
((-90 - A) * 100) / (B - A) = ((-90 - (-100)) * 100) / (100 - (-100))
= 10 * 100 / 200
= 5
An input value of 50 results in a slider value of:
((50 - A) * 100) / (B - A) = ((50 - (-100)) * 100) / (100 - (-100))
= 150 * 100 / 200
= 75
I don't know C++, but I do know Math, so try:
val_slider = 100 * ( val_textin - A ) / ( B - A )
Hey wait. That's exactly what you have. Test case..
A=-200, B=+200, val_texin = 100 (75% of bar, right?)
val_slider = 100 * ( 100 - -200 ) / ( 200 - - 200 )
= ( 300 ) / ( 400 ) * 100
= 75
See, you got it right. The only thing that COULD happen is B==A, but that can't be accounted for with math and requires a single IF. If they are equal, val_slider is exactly B (or A, as they are equal).