HR30 Day 25: Running Time and Complexity

1 개요[ | ]

HR30 Day 25: Running Time and Complexity

해커랭크 30 Days of Code
문제 풀이
20-29 Day e
HR30 Day 20: Sorting

HR30 Day 21: Generics

HR30 Day 22: Binary Search Trees

HR30 Day 23: BST Level-Order Traversal

HR30 Day 24: More Linked Lists

HR30 Day 25: Running Time and Complexity

HR30 Day 26: Nested Logic

HR30 Day 27: Testing

HR30 Day 28: RegEx, Patterns, and Intro to Databases

HR30 Day 29: Bitwise AND

2 Java[ | ]

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n-- > 0) {
            int num = sc.nextInt();
            if( isPrime(num) ) System.out.println("Prime");
            else System.out.println("Not prime");
        }
    }
    private static boolean isPrime(int n) {
        if( n<2 ) return false;
        for( int i=2; i<=(int)Math.sqrt(n); i++)
            if( n%i == 0) return false;
        return true;
    }
}

3 PHP[ | ]

<?php
function is_prime($n) {
    if( $n<2 ) return false;
    for($i=2; $i<=sqrt($n); $i++) if($n%$i == 0) return false;
    return true;
}
$_fp = fopen("php://stdin", "r");
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
fscanf($_fp, '%d', $n);
while($n-- > 0)  {
    fscanf($_fp, '%d', $num);
    if( is_prime($num) ) echo "Prime\n";
    else echo "Not prime\n";
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}