Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 17 December 2022

Task and Solution : Project Euler #3: Largest prime factor (HackerRank)

 This problem is a programming version of Problem 3 from projecteuler.net

The prime factors of  are  and .

What is the largest prime factor of a given number ?

Input Format

First line contains , the number of test cases. This is followed by  lines each containing an integer .

Constraints

Output Format

For each test case, display the largest prime factor of .

Sample Input 0

2
10
17

Sample Output 0

5
17

Explanation 0

  • Prime factors of  are , largest is .
  • Prime factor of  is  itself, hence largest is .

Solution

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package hackerrank.contest.projecteuler;

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;


/**
 *
 * @author surachman
 */
public class Largest_prime_factor1 {
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      int t = in.nextInt();
      long max=0;
      for(int a0 = 0; a0 < t; a0++){
          long n = in.nextLong();
          long div = 2;

          while (div <= Math.floor(Math.sqrt(n))) {
            if (n % div== 0) {
              n /= div;

            } else {
              div++;
            }
          }
         System.out.println(n);
        }
    }
   
}


Source : HackerRank

Friday, 16 December 2022

Task and Solution : ProjectEuler+Project Euler #2: Even Fibonacci numbers in Java (HackerRank)

 

This problem is a programming version of Problem 2 from projecteuler.net

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with  and , the first  terms will be:

By considering the terms in the Fibonacci sequence whose values do not exceed , find the sum of the even-valued terms.

Input Format

First line contains  that denotes the number of test cases. This is followed by  lines, each containing an integer, .

Constraints

Output Format

Print the required answer for each test case.

Sample Input 0

2
10
100

Sample Output 0

10
44

Explanation 0

  • For , we have , sum is .
  • For , we have , sum is .


Solution

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package hackerrank.contest.projecteuler;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
/**
 *
 * @author surachman
 */
public class Even_Fibonacci_numbers_1 {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int i = 0; i < t; i++){
            long n = sc.nextLong();
            long a = 0;
            long b = 2;
            long ans = b;
           
            while(4*b + a <= n){
                ans += 4*b + a;
                long temp = a;
                a = b;
                b = 4*b + temp;
            }
            System.out.println(ans);
        }
    }
}


Source : HackerRank