Max to 0 to Max algorithm - math

First off, let me warn you that my Maths knowledge is fairly limited (hence my coming here to ask about this).
It's a silly example, but what I essentially want is to have a variable number of rows, and be able to set a maximum value, then for each progressive row decrease that value until half way at which point start increasing the value again back up to the maximum by the final row.
To illustrate this... Given that: maxValue = 20, and rows = 5, I need to be able to get to the following values for the row values (yes, even the 0):
row 1: 20
row 2: 10
row 3: 0
row 4: 10
row 5: 20
There are limitations though because I'm trying to do this in Compass which uses SASS. See here for the available operations, but to give you the gist of it, only basic operations are available.
I'm able to loop through the rows, so just need the calculation that will work for each individual row in the series. This is the kind of loop I'm able to use:
$maxValue:20;
$rows:5;
#for $i from 1 through $rows {
// calculation here.
}

I haven't really worked with SASS before, but try something like this using a basic if and the floor function, not sure if will work
// set various variables
$maxValue:20;
$rows:5;
// get row number before middle row, this instance it will be 2
$middleRow = floor( $maxValue / $rows )
// get increment amount, this instance should be 10
$decreaseValue = ( max / floor( rows / 2) )
#for $i from 0 through $rows - 1 {
#if $i <= $middleRow {
( $maxValue- ( $decreaseValue * $i ) )
}
#else{
// times by -1 to make positive value
( $maxValue - ( $decreaseValue * -$i ) ) * -1
}
}
I have tested the above in js ( with relative syntax ) and it yielded the required results ( jsBin example ). Hope this helps

Related

Gravityform easy calculation - but total is not beginning from 0

I have a pretty simple calculation in gravityform, but because I add +1 and +0.5 to what people are filling out in the input fields, then the total from the begining is showing a number instead of only 0.
Example:
Field A +1
Field B +0.5
Calculation Field A x B x 295. But because I add +1 and +0.5 to what people put in the input field, then the total from the beginning, shows 147,5. Because it calculate 1*.0.5*295 = 147,5.
But I want the total just to show 0 until people are filling out the input field.
How can I avoid this?
Your best bet for a code-based solution will probably be to use the gform_calculation_result filter and check a value of 1 or 0.5 and return 0 instead.
gform.addFilter( 'gform_calculation_result', function( result, formulaField, formId, calcObj ) {
if ( result == 1 || result == 0.5 ) {
result = 0;
}
return result;
} );
An alternate approach would be to use conditional statements right in the calculation formula, powered by our Gravity Forms Advanced Calculations plugin.
You could check if the field has a value and provide a formula for that and return 0 otherwise.
if ( F1 > 0 ):
F1 + 1
else:
0
endif;

I want to fill a matrix using a loop returning the value of a function where the argumentof the function also change ith the loop

So here is my function :
combination<-function(t,s,T,H,l) {
for(i in 0:floor((T-t)/s)){
a[i+1] <- ( ((-1)^i) * factorial(t)/( factorial(t-i)*factorial(i) ) * factorial(T-s*i-1)/( factorial(T-s*i-t)*factorial(t-1) ) )
b<-cumsum(a)
}
for(i in 0:floor((H-l)/s)){
r[i+1] <- ( ((-1)^i) * factorial(l)/( factorial(l-i)*factorial(i) ) * factorial(H-s*i-1)/( factorial(H-s*i-l)*factorial(l-1) ) )
c<-cumsum(l)
}
return (tail(b-((t*(t-1))/2)*c,n=1))
}
It calculates the total number of combinations leading to a given total if using a defined number of dice "t". The dice have 5 sides from two to six.
I want to create a matrix showing the number of combination for each total given a number of dice.
exemple: For the total of 10, i would find at any number of given 2-6 dices the total number of combinations leading to it.
For 3 it's 15 combinations, for two it's 3 and so on.
I want this informations displayed in a matrix for all potential values up to 20 dices.
I have tried :
for(i in 1:t) {
for(j in 1:6*t) {
M[j,i]<-combination(i,6,j)
}
return(M)
}
}
I'm a bit of a newby and even if i understand (i think) why it doesn't work, i don't know how to make it work :(
Thanks for any help :D

I want to calculate the timedifference between to times

I want to calculate the difference of two columns of a dataframe containing times. Since not always a value from the same column ist bigger/later, I have to do a workaround with an if-clause:
counter = 1
while(counter <= nrow(data)){
if(data$time_end[counter] - data$time_begin[counter] < 0){
data$chargingDuration[counter] = 1-abs(data$time_end[counter]-data$time_begin[counter])
}
if(data$time_end[counter] - data$time_begin[counter] > 0){
data$chargingDuration[counter] = data$time_end[counter]-data$time_begin[counter]
}
counter = counter + 1
}
The output I get is a decimalvalue smaller than 1 (i.e.: 0,53322 meaning half a day)... However, if I use my console and calculate the timedifference manually for a single line, I get my desired result looking like 02:12:03...
Thanks for the help guys :)

Generating random math equation using tree - Avoiding Parentheses

Edit: This was originally on programmers.stackexchange.com, but since I already had some code I thought it might be better here.
I am trying to generate a random math problem/equation. After researching the best (and only) thing I found was this question: Generating random math expression. Because I needed all 4 operations, and the app that I will be using this in is targeted at kids, I wanted to be able to insure that all numbers would be positive, division would come out even, etc, I decided to use a tree.
I have the tree working, and it can generate an equation as well as evaluate it to a number. The problem is that I am having trouble getting it to only use parentheses when needed. I have tried several solutions, that primaraly involve:
Seeing if this node is on the right of the parent node
Seeing if the node is more/less important then it's parent node (* > +, etc).
Seeing if the node & it's parent are of the same type, and if so if the order matters for that operation.
Not that it matters, I am using Swift, and here is what I have so far:
func generateString(parent_node:TreeNode) -> String {
if(self.is_num){
return self.equation!
}
var equation = self.equation!
var left_equation : String = self.left_node!.generateString(self)
var right_equation : String = self.right_node!.generateString(self)
// Conditions for ()s
var needs_parentheses = false
needs_parentheses = parent_node.importance > self.importance
needs_parentheses = (
needs_parentheses
||
(
parent_node.right_node?.isEqualToNode(self)
&&
parent_node.importance <= self.importance
&&
(
parent_node.type != self.type
&&
( parent_node.order_matters != true || self.order_matters != true )
)
)
)
needs_parentheses = (
needs_parentheses
&&
(
!(
self.importance > parent_node.importance
)
)
)
if (needs_parentheses) {
equation = "(\(equation))"
}
return equation.stringByReplacingOccurrencesOfString("a", withString: left_equation).stringByReplacingOccurrencesOfString("b", withString: right_equation)
}
I have not been able to get this to work, and have been banging my head against the wall about it.
The only thing I could find on the subject of removing parentheses is this: How to get rid of unnecessary parentheses in mathematical expression, and I could not figure out how to apply it to my use case. Also, I found this, and I might try to build a parser (using PEGKit), but I wanted to know if anybody had a good idea to either determine where parentheses need to go, or put them everywhere and remove the useless ones.
EDIT: Just to be clear, I don't need someone to write this for me, I am just looking for what it needs to do.
EDIT 2: Since this app will be targeted at kids, I would like to use the least amount of parentheses possible, while still having the equation come out right. Also, the above algorithm does not put enough parentheses.
I have coded a python 3 pgrm that does NOT use a tree, but does successfully generate valid equations with integer solutions and it uses all four operations plus parenthetical expressions. My target audience is 5th graders practicing order of operations (PEMDAS).
872 = 26 * 20 + (3 + 8) * 16 * 2
251 = 256 - 3 - 6 + 8 / 2
367 = 38 * 2 + (20 + 15) + 16 ** 2
260 = 28 * 10 - 18 - 4 / 2
5000 = 40 * 20 / 4 * 5 ** 2
211 = 192 - 10 / 2 / 1 + 24
1519 = 92 * 16 + 6 + 25 + 16
If the python of any interest to you ... I am working on translating the python into JavaScript and make a web page available for students and teachers.

Groovy collections - Finding an element that matches a condition

I have a groovy collections which is an array, containing value starting from 0 through 'n'. I need to find a particular array index when a series of conditions occured. And,I do not need to scan through every value of the array but can jump across pre-defined intervals. For example, look for the condition for every 10 values in the array. Can someone tell me a way to do this?
For example, I want to do somehting like this below
def alltimes = [0 . . . . . 10000]
def end_time = 10000
def time = 0
while(time <= end_time)
{
// check the condition for alltimes[time]
if(condition_satisfied){
println "condition satisfied at time ${time}"
break
}
time = time + 50
}
When i explored all available methods of array, i did not find any one which can allow to jump variables instead of just one as in methods each, eachwithindex.
Seems like I need to use metaclass and create a new method?
You can use find for this:
def allTimes = 0..10000
Closure<Boolean> checkCondition = { all, single ->
single > 300
}
​(0..10000).step( 50 )​.find { time -> ​checkCondition( allTimes, time ) }​
Which is ripe for currying:
def allTimes = 0..10000
Closure<Boolean> checkCondition = { all, single ->
single > 300
}
​(0..10000).step( 50 )​.find checkCondition.curry( allTimes )​

Resources