Skip to main content

Java 8 coding challenge: Life, the Universe, and Everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
SAMPLE INPUT
1
2
88
42
99

SAMPLE OUTPUT
1
2
88

 

Code:

import java.util.*;
import java.io.*;
import java.math.BigInteger;
 
public class Tester
{
 public static long mod=(long)(1e9+7);
 public static long xx,yy,d;
 static int minPrime[];
 static boolean isPrime[];
 
 public static void main(String[] args) throws IOException 
 {
 InputReader s=new InputReader(System.in);
 OutputStream outputStream = System.out;
 PrintWriter out=new PrintWriter(outputStream);
 
 while(true)
 {
 int n=s.nextInt();
 if(n==42)
 break;
 else
 out.println(n);
 }
 
 
 out.close();
 } 
 
 static void modifiedSieve(int n)
 {
 minPrime = new int[n+1];
 
 for(int i=2; i<=Math.sqrt(n); i++)
 {
 if(minPrime[i] == 0)
 {
 for(int j=i*i; j<=n; j=j+i)
 {
 if(minPrime[j] == 0)
 minPrime[j] = i;
 }
 }
 }
 
 for(int i=2; i<=n; i++)
 {
 if(minPrime[i] == 0)
 minPrime[i] = i;
 }
 
 }
 
 static void Sieve(int n)
 {
 isPrime = new boolean[n+1];
 Arrays.fill(isPrime, true);
 
 isPrime[0]=false;
 isPrime[1]=false;
 
 for(int i=2; i<=Math.sqrt(n); i++)
 {
 if(isPrime[i])
 {
 for(int j=i*i; j<=n; j=j+i)
 isPrime[j]=false;
 }
 }
 
 }
 
 static void extendedEuclid(long A, long B)
 {
 if(B == 0) {
 d = A;
 xx = 1;
 yy = 0;
 }
 else {
 extendedEuclid(B, A%B);
 long temp = xx;
 xx = yy;
 yy = temp - (A/B)*yy;
 }
 }
 
 static long gcd(long a,long b)
 {
 if(b==0)
 return a;
 
 return gcd(b,a%b);
 }
 
 static long modulo(long x,long y,long p) 
 {
 long res = 1;
 x = x % p; 
 
 while (y > 0)
 {
 if (y%2==1)
 res = (res*x) % p;
 
 y = y>>1;
 x = (x*x) % p; 
 }
 return res;
 }
 
 public static void debug(Object... o)
 {
 System.out.println(Arrays.deepToString(o));
 }
 
 static long exp(long a, long b)
 {
 if(b==0)
 return 1;
 if(b==1)
 return a;
 if(b==2)
 return a*a;
 
 if(b%2==0)
 return exp(exp(a,b/2),2);
 else
 return a*exp(exp(a,(b-1)/2),2);
 }
 
 static class Pair implements Comparable<Pair>
 {
 long x,y;
 Pair(long ii, long cc)
 {
 x=ii;
 y=cc;
 }
 
 public int compareTo(Pair o) 
 {
 return Long.compare(this.x, o.x);
 }
 
 public int hashCode() {
 int hu = (int) (x ^ (x >>> 32));
 int hv = (int) (y ^ (y >>> 32));
 return 31 * hu + hv;
 }
 
 public boolean equals(Object o) {
 Pair other = (Pair) o;
 return x == other.x && y == other.y;
 }
 
 public String toString() {
 return "[x=" + x + ", y=" + y + "]";
 }
 
 }
 
 static final class InputReader{
 private final InputStream stream;
 private final byte[] buf=new byte[1024];
 private int curChar;
 private int Chars;
 public InputReader(InputStream stream){this.stream=stream;}
 private int read()throws IOException{
 if(curChar>=Chars){
 curChar=0;
 Chars=stream.read(buf);
 if(Chars<=0)
 return -1;
 }
 return buf[curChar++];
 }
 public final int nextInt()throws IOException{return (int)nextLong();}
 public final long nextLong()throws IOException{
 int c=read();
 while(isSpaceChar(c)){
 c=read();
 if(c==-1) throw new IOException();
 }
 boolean negative=false;
 if(c=='-'){
 negative=true;
 c=read();
 }
 long res=0;
 do{
 if(c<'0'||c>'9')throw new InputMismatchException();
 res*=10;
 res+=(c-'0');
 c=read();
 }while(!isSpaceChar(c));
 return negative?(-res):(res);
 }
 public final int[] nextIntBrray(int size)throws IOException{
 int[] arr=new int[size];
 for(int i=0;i<size;i++)arr[i]=nextInt();
 return arr;
 }
 public final String nextString()throws IOException{
 int c=read();
 while(isSpaceChar(c))c=read();
 StringBuilder res=new StringBuilder();
 do{
 res.append((char)c);
 c=read();
 }while(!isSpaceChar(c));
 return res.toString();
 }
 public final String nextLine()throws IOException{
 int c=read();
 while(isSpaceChar(c))c=read();
 StringBuilder res=new StringBuilder();
 do{
 res.append((char)c);
 c=read();
 }while(c!='\n'&&c!=-1);
 return res.toString();
 }
 private boolean isSpaceChar(int c)
 {
 return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
 }
 }
 
}

Comments

Popular posts from this blog

Terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. The key features of Terraform are: Infrastructure as Code : Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and

Java 8 coding challenge: Roy and Profile Picture

Problem:  Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. Minimum dimension of the picture can be  L x L , where  L  is the length of the side of square. Now Roy has  N  photos of various dimensions. Dimension of a photo is denoted as  W x H where  W  - width of the photo and  H  - Height of the photo When any photo is uploaded following events may occur: [1] If any of the width or height is less than L, user is prompted to upload another one. Print " UPLOAD ANOTHER " in this case. [2] If width and height, both are large enough and (a) if the photo is already square then it is accepted. Print " ACCEPTED " in this case. (b) else user is prompted to crop it. Print " CROP IT " in this case. (quotes are only for clarification) Given L, N, W and H as input, print appropriate text as output. Input: First line contains  L . Second line contains  N , number of

Salt stack issues

The function “state.apply” is running as PID Restart salt-minion with command:  service salt-minion restart No matching sls found for ‘init’ in env ‘base’ Add top.sls file in the directory where your main sls file is present. Create the file as follows: 1 2 3 base: 'web*' : - apache If the sls is present in a subdirectory elasticsearch/init.sls then write the top.sls as: 1 2 3 base: '*' : - elasticsearch.init How to execute saltstack-formulas create file  /srv/pillar/top.sls  with content: base : ' * ' : - salt create file  /srv/pillar/salt.sls  with content: salt : master : worker_threads : 2 fileserver_backend : - roots - git gitfs_remotes : - git://github.com/saltstack-formulas/epel-formula.git - git://github.com/saltstack-formulas/git-formula.git - git://github.com/saltstack-formulas/nano-formula.git - git://github.com/saltstack-f