Skip to main content
while -> for
Source Link
mazzy
  • 7.2k
  • 2
  • 12
  • 22

Powershell, 5959 58 bytes

param($n,$m)$n..$m|%{whilefor($_;$_){$s+=$_-band1;$_=$_-shr1}};$s

Test script:

$f = {
param($n,$m)$n..$m|%{whilefor($_;$_){$s+=$_-band1;$_=$_-shr1}};$s
}

@(
,(4,7,8)
,(10,20,27)
,(100,200,419)
,(1,3,4)
,(1,2,2)
,(1000,2000,5938)
) | % {
    $n,$m,$e=$_
    $r = &$f $n $m
    $c = $r -eq $e

    "$c : $n , $m ---> $e = $r"
}

Output:

True : 4 , 7 ---> 8 = 8
True : 10 , 20 ---> 27 = 27
True : 100 , 200 ---> 419 = 419
True : 1 , 3 ---> 4 = 4
True : 1 , 2 ---> 2 = 2
True : 1000 , 2000 ---> 5938 = 5938

Powershell, 59 bytes

param($n,$m)$n..$m|%{while($_){$s+=$_-band1;$_=$_-shr1}};$s

Test script:

$f = {
param($n,$m)$n..$m|%{while($_){$s+=$_-band1;$_=$_-shr1}};$s
}

@(
,(4,7,8)
,(10,20,27)
,(100,200,419)
,(1,3,4)
,(1,2,2)
,(1000,2000,5938)
) | % {
    $n,$m,$e=$_
    $r = &$f $n $m
    $c = $r -eq $e

    "$c : $n , $m ---> $e = $r"
}

Output:

True : 4 , 7 ---> 8 = 8
True : 10 , 20 ---> 27 = 27
True : 100 , 200 ---> 419 = 419
True : 1 , 3 ---> 4 = 4
True : 1 , 2 ---> 2 = 2
True : 1000 , 2000 ---> 5938 = 5938

Powershell, 59 58 bytes

param($n,$m)$n..$m|%{for(;$_){$s+=$_-band1;$_=$_-shr1}};$s

Test script:

$f = {
param($n,$m)$n..$m|%{for(;$_){$s+=$_-band1;$_=$_-shr1}};$s
}

@(
,(4,7,8)
,(10,20,27)
,(100,200,419)
,(1,3,4)
,(1,2,2)
,(1000,2000,5938)
) | % {
    $n,$m,$e=$_
    $r = &$f $n $m
    $c = $r -eq $e

    "$c : $n , $m ---> $e = $r"
}

Output:

True : 4 , 7 ---> 8 = 8
True : 10 , 20 ---> 27 = 27
True : 100 , 200 ---> 419 = 419
True : 1 , 3 ---> 4 = 4
True : 1 , 2 ---> 2 = 2
True : 1000 , 2000 ---> 5938 = 5938
Source Link
mazzy
  • 7.2k
  • 2
  • 12
  • 22

Powershell, 59 bytes

param($n,$m)$n..$m|%{while($_){$s+=$_-band1;$_=$_-shr1}};$s

Test script:

$f = {
param($n,$m)$n..$m|%{while($_){$s+=$_-band1;$_=$_-shr1}};$s
}

@(
,(4,7,8)
,(10,20,27)
,(100,200,419)
,(1,3,4)
,(1,2,2)
,(1000,2000,5938)
) | % {
    $n,$m,$e=$_
    $r = &$f $n $m
    $c = $r -eq $e

    "$c : $n , $m ---> $e = $r"
}

Output:

True : 4 , 7 ---> 8 = 8
True : 10 , 20 ---> 27 = 27
True : 100 , 200 ---> 419 = 419
True : 1 , 3 ---> 4 = 4
True : 1 , 2 ---> 2 = 2
True : 1000 , 2000 ---> 5938 = 5938