Created
December 5, 2018 09:10
-
-
Save computingfreak/cde68b305197aced203d2c3d21a56a16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class series | |
{ | |
public static void main(int n) { | |
for(int i=1;i<=n;i++){ | |
//System.out.println(operate(i)); | |
System.out.println(i+" -> "+op(i)); | |
} | |
} | |
public static int operate(int n){ | |
List<Integer> deck = new ArrayList<Integer>(); | |
//1 at top, | |
//N at bottom | |
for(int i=1;i<=n;i++){ | |
deck.add(i); | |
} | |
while(deck.size()>=2) { | |
deck.remove(0); | |
deck.add(deck.remove(0)); | |
} | |
return deck.get(0); | |
} | |
public static int op(int n) { | |
if((n & (n - 1)) == 0){ | |
return n; | |
} else { | |
int k=(int)Math.floor((Math.log(n)/(1.0*Math.log(2))))+1; | |
return (int)(Math.pow(2,k)-2*(Math.pow(2,k)-n)); | |
} | |
} | |
public static int finalnum(int n){ | |
if(n<2)return n; | |
else { | |
if(n<finalnum(n-1)+2){ | |
return 2; | |
} else { | |
return finalnum(n-1)+2; | |
} | |
} | |
} | |
public static int exp(int a,int b) | |
{ | |
int e=1; | |
for(int i=0;i<b;i++) | |
{ | |
e=e*a; | |
} | |
return e; | |
} | |
public static int finaln(int num) { | |
int alive=0,k; | |
for(int i=0;i<num;i++) | |
{ | |
k=exp(2,i); | |
if(k>num) | |
{ | |
k=exp(2,i-1); | |
int x=num-k; | |
alive=(2*x)+1; | |
break; | |
} | |
} | |
return alive; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment