Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Math 如何找到给定数字、间隔和起始值的范围?_Math_Algebra - Fatal编程技术网

Math 如何找到给定数字、间隔和起始值的范围?

Math 如何找到给定数字、间隔和起始值的范围?,math,algebra,Math,Algebra,提供以下值 起始值=1 结束值=20 间隔=5 我得到了一个数字6。我必须找到数字6落下的数字范围,现在答案是6-10 如果给定的数字大于结束值,则返回相同的数字 是否有公式可以生成数字的范围 更新 我尝试了下面的解决方案,但是如果范围间隔改变,它就不起作用了 $end_value = $start_value + $range_interval; // we blindly return the last term if value is greater than max value if

提供以下值

  • 起始值=1
  • 结束值=20
  • 间隔=5
  • 我得到了一个数字6。我必须找到数字6落下的数字范围,现在答案是6-10

    如果给定的数字大于结束值,则返回相同的数字

    是否有公式可以生成数字的范围

    更新

    我尝试了下面的解决方案,但是如果范围间隔改变,它就不起作用了

     $end_value = $start_value + $range_interval;
    
    // we blindly return the last term if value is greater than max value
    if ($input_num > $end_value) {
      return '>' . $end_value;
    }
    
    // we also find if its a first value
    if ($input_num <= $end_value && $value >= $start_value) {
      return $start_value . '-' . $end_value;
    }
    
    // logic to find the range for a given integer
    $dived_value = $input_num/$end_value;
    
    // round the value to get the exact match
    $rounded_value = ceil($dived_value);
    
    $upper_bound_range = $rounded_value*$end_value;
    
    $lower_bound_range = $upper_bound_range - $end_value;
    
    return $lower_bound_range . '-'. $upper_bound_range;
    
    $end\u value=$start\u value+$range\u interval;
    //如果值大于最大值,则盲目返回最后一项
    如果($input\u num>$end\u值){
    返回“>”.$end_值;
    }
    //我们还发现它是否是第一个值
    如果($input\U num=$start\U value){
    返回$start\u值。“-”.$end\u值;
    }
    //查找给定整数范围的逻辑
    $dived_value=$input_num/$end_value;
    //将值四舍五入以获得精确匹配
    $rounded\u value=ceil($dived\u value);
    $upper_bound_range=$rounded_value*$end_value;
    $lower_bound_range=$lower_bound_range-$end_value;
    返回$lower_bound_range'.-'$上限范围;
    
    在(c样式)伪代码中:

    // Integer division assumed
    rangeNumber = (yourNumber - startValue) / rangeLength;
    lower_bound_range = startValue + rangeNumber*rangeLength;
    upper_bound_range = lower_bound_range + rangeLength-1;
    
    请输入:

    rangeNumber = (6-1)/5 = 1
    lower_bound_range = 1 + 5*1 = 6
    upper_bound_range = 10
    

    所以范围是[6,10]

    答案取决于你谈论的是整数还是浮点数。因为你们所有的例子数字都是整数,我想你们会谈论这些。我进一步假设所有区间包含相同数量的整数,在示例5中,即1…5、6…10、11…15和16…20。请注意,0不包含在第一个间隔中(否则第一个间隔有6个数字)。
    在这种情况下,答案很简单。
    顺其自然:
    s第一个间隔中不包含的起始值,
    i区间大小,即其包含的整数数,
    p应分配间隔的提供编号,
    b此间隔中的第一个整数,且
    e此间隔中的最后一个整数。
    然后:
    b=s+(p-s-1)\i*i+1(此处,“\”表示整数除法,即无余数)
    e=b+i-1
    在您的示例中:
    s=0,i=5,p=6,因此
    b=0+(6-0-1)\5*5+1=6

    e=6+5-1=10

    应该在上问这个问题。如果包括起始值0,范围不是5-9吗?@Vladimir我已经用我的try@Milind感谢我更改了起始值,但它不适用于其他值,如startValue:100,Interval(rangeLength):100,EndValue:500 rangeNumber=(220-100)/100=1.2下限范围=100+(1.2*100)=220上限范围=220+(100-1)=319@DineshKumar,对不起,我假设语言默认使用整数除法(例如c),所以(220-100)/100=1。检查如何使用您使用的语言执行整数除法