ChocolatesByNumbers

There are N chocolates in a circle. Count the number of chocolates you will eat.


Solution:

Solution to Codility's Chocolates By Numbers problem which is from the Codility Lesson 12: Euclidean algorithm and, is solved in Java 8 with 100% performance and correctness scores. The goal here is to there are n chocolates in a circle. count the number of chocolates you will eat. You can find the question of this ChocolatesByNumbers problem in the Codility website.


package Codility.Lesson12;

import java.util.*;

public class ChocolatesByNumbers {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a1 = 10;
		int a2 = 4;
		int result2 = solution(a1, a2);
		System.out.println(result2);
	}

	public static int solution(int N, int M) {
		   return N / gcdByDivision(N, M);
	  }
	 
	 public static int gcdByDivision(int A, int B) {
	    if(A % B == 0) return B;
	    else           return gcdByDivision(B, A % B);
	  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *