Highest Divisor Solution Codechef

Highest Divisor Solution February Challenge 2021

You are given an integer NN. Find the largest integer between 11 and 1010 (inclusive) which divides NN.

Also See: February Long Challenge 2021 Solutions

Input

The first and only line of the input contains a single integer NN.

Output

Print a single line containing one integer ― the largest divisor of NN between 11 and 1010.

Constraints

  • 2≤N≤1,0002≤N≤1,000

Subtasks

Subtask #1 (100 points): original constraints

Example Input 1

24

Example Output 1

8

Explanation

The divisors of 2424 are 1,2,3,4,6,8,12,241,2,3,4,6,8,12,24, out of which 1,2,3,4,6,81,2,3,4,6,8 are in the range [1,10][1,10]. Therefore, the answer is max(1,2,3,4,6,8)=8max(1,2,3,4,6,8)=8.

Example Input 2

91

Example Output 2

7

Explanation

The divisors of 9191 are 1,7,13,911,7,13,91, out of which only 11 and 77 are in the range [1,10][1,10]. Therefore, the answer is max(1,7)=7max(1,7)=7.

Follow us on telegram for quick update an abundance of free knowledge: Click Here

Solution

Program C:

#include<stdio.h>
int main() {
    long long int n,i;
    scanf("%lld",&n);
    i=10;
    while(n % i)
    i--;
    printf("%lld" ,i);
    
}

Program C++:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int MAXN = 1e6 + 10;
const int mod = 1e9 + 7;

int main() 
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int ans = 1;
	for (int i = 1; i <= 10; i++) {
		if (n % i == 0) ans = i;
	}
	cout << ans << endl;
    return 0;
}

Program Java:

import java.util.*;

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
int n,i;
Scanner in=new Scanner(System.in);
n=in.nextInt();
for(i=10;i>0;i--)
{
   if(n%i==0)
   {
       System.out.println(i);
       break;
   }
}
}
}

Program Python:

n = int(input())
list1 = []
for i in range(1,10):
    if n%i==0:
        list1.append(i)
print(max(list1))

Codechef Long Challenges

September Long Challenge 2021 Solution

February Long Challenge 2021

Leave a Comment