🌙 DARK

Maths Challenge Program in java

 

Maths Challenge

 

In connection to the International Mathematics Day celebration, the Mathematical Scholars Society had arranged for a Maths Challenge Event where school students participated in large numbers. The first level of the challenge was an oral quiz, followed by a written test in the second round.


In the second round, the problem that the students had to answer, goes like this:
For every positive number ‘n’ we define a function streak(n)=k as the smallest positive integer k such that n+k is not divisible by k+1.

 

For example:

13 is divisible by 1
14 is divisible by 2
15 is divisible by 3
16 is divisible by 4
17 is NOT divisible by 5
So streak(13)=4.
Similarly:
120 is divisible by 1
121 is NOT divisible by 2
So streak(120)=1.

Now, define P(k, N) which will give the number of integers n, 1<n<=N, for which streak(n) = k. Write a program to get the input as 'k' and 'N' and,
find the count of integers until N which has the streak value as 'k'. 


Input Format:

The first line of the input is an integer ‘k’ which is the streak value of an integer n.
The second line of the input is an integer ‘N’ which is the upper limit of numbers until which P(k, N) is calculated.


Output Format:

The output is an integer that gives the count of integers until ‘N’ which has the streak value as ‘k’.

Refer to sample input and output for formatting specifications.


Sample Input 1:

3
14


Sample Output 1:

1

Explanation:

If s=3 and N=14.
If we start computing streak value for the integers from 1 to N,
For 1,
1 is divisible by 1
2 is divisible by 2
3 is divisible by 3
4 is divisible by 4 
so the streak value of 1 is more than 3.
For 2,
2 is divisible by 1
3 is NOT divisible by 2
so the streak of 2 is 1.
likewise, we can see only the integer 7 has the streak value of 3 within 14, because
7 is divisible by 1
8 is divisible by 2
9 is divisible by 3
10 is NOT divisible by 4
Hence streak(7) = 3.
So P(3, 14) = 1 and so the output is 1.


Sample Input 2:

1
10


Sample Output 2:

5

Explanation:

The 5 Numbers which has streak 1(k)  Within 10(N)  are 2,4,6,8,10.

PROGRAM:-
@MRProgrammer89 


CREATE CLASS < Main :


import java.util.Scanner;

public class Main{

    public static void main(String[] args){         

           //Fill your code

           Scanner sc = new Scanner(System.in);

           

           int k,n,streak=0,temp,count=0;

           

           k = sc.nextInt();

           n = sc.nextInt();

           

           int num =1;

           for(int i=1;i<=n;i++)

           {

               temp = i;

               while(true)

               {

                   if((temp%num)==0)

                   {

                       streak++;

                       temp++;

                       num++;

                   }

                   else{

                       break;

                   }

                   

                   if(streak>k)

                    break;

                    

                

               }

               num=1;

               if(streak==k)

                count++;

                

                streak=0;

           }

           

           System.out.println(count);

    }

}


HOMEWORK: 

1. Write a program in Java to check if a number is even or odd in Java? (input 2 output true, input 3: output false)
A number is called even if it is completely divisible by two and odd if it's not entirely divisible by two. For example, the number 4 is an even number because when you do 4/2, the remainder is 0, which means 4 is completely divisible by 2. On the other hand, 5 is an odd number because 5/2 will result in the remainder as 1. See here to find how to check even and odd numbers in Java.



2. Write a program in Java to find out if a number is prime in Java? (input 7: output true, input 9: output false)
A number is called prime if it is divisible by either itself or 1. There are many algorithms to find prime numbers like, instead of dividing till number, division up to the square root of a number may be enough. Start from the simplest one and then try to solve this problem in a couple of different ways. Here is one way to check prime numbers in Java



3. Write a Java program to check if a number is a palindrome in Java? ( 121 is a palindrome, 321 is not)
A number is called a palindrome if the number is equal to the reverse of a number e.g., 121 is a palindrome because the reverse of 121 is 121 itself. On the other hand 321 is not a palindrome because the reverse of 321 is 123, which is not equal to 321. See here for a solution of checking if a number is a palindrome or not in Java.




@MRProgrammer89 






MOTIVATION :


    “The way we spend our time defines who we are 


Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


VISITE MORE BLOGS




THANK YOU....😊


Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form