Sa se calculeze suma 2^0+2^1+...+2^n
package ro.utcluj.poo.lab02;Cu functie si test
import java.math.BigInteger;
public class Suma2n {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger s,p,c;
/*s=new BigInteger("2");
c=new BigInteger("-1");
s = s.pow(64);
s = s.add(c);
System.out.println(s.toString());*/
p=new BigInteger("1");
c=new BigInteger("2");
s=new BigInteger("0");
int i,n;
n=63;
for(i=0;i<=n;i++)
{
s=s.add(p);
p=p.multiply(c);
//System.out.println(s.longValue());
}
System.out.println(s.longValue());
}
}
package ro.utcluj.poo.lab02;Test
import java.math.BigInteger;
public class Suma2n {
public int putere(double n){
int s,p,c;
int i;
s=0;
p=1;
c=2;
for(i=0;i<=n;i++)
{
s=s+p;
p=p*c;
}
return s;
}
public static void main(String[] args) {
}
}
package ro.utcluj.poo.lab02;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import static org.testng.Assert.*;
public class Suma2nTest {
Suma2n main;
@BeforeClass
public void init(){
main = new Suma2n();
}
@DataProvider
public Object[][] valoriDeTest(){
return new Double[][]{
{2.0, 7.0, 0.001},
{3.0,15.0, 0.01},
{4.0,31.0,0.01}
};
}
@Test(dataProvider = "valoriDeTest")
public void get(double input, double expected, double precision) {
double result = main.putere(input);
assertEquals(result, expected,precision);
}
}