Problem
Given a positive integer n (1 <= n <= 10^18). Check whether a number has exactly three distinct factors, return true if it has exactly three distinct factors, otherwise false.
ExampleGiven n = 9, return true
Number 9 has exactly three factors: 1, 3, 9, so return true.
Given n = 10, return false
Solutionpublic class Solution { /** * @param n: the given number * @return: return true if it has exactly three distinct factors, otherwise false */ public boolean isThreeDisctFactors(long n) { // write your code here long fac = (long) Math.sqrt(n); if (fac * fac != n) return false; for (long i = 2; i < fac; i++) { if (n % i == 0) return false; } return true; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/69470.html
摘要:用動規方法做建立長度為和的二維數組,表示的第到位子串包含不同的的第到位子串的個數。初始化當的子串長度為時,當的子串長度為時,當和子串都為時,包含,故。 Problem Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a strin...
Problem Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example Input: 2Output: 987Ex...
Problem Write a program to check whether a given number is an ugly number`. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly ...
摘要:建兩個新數組,一個存數,一個存。數組中所有元素初值都是。實現的過程是,一個循環里包含兩個子循環。兩個子循環的作用分別是,遍歷數組與相乘找到最小乘積存入再遍歷一次數組與的乘積,結果與相同的,就將加,即跳過這個結果相同結果只存一次。 Problem Write a program to find the nth super ugly number. Super ugly numbers a...
Problem Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute dif...
閱讀 1002·2021-09-30 09:58
閱讀 2829·2021-09-09 11:55
閱讀 2001·2021-09-01 11:41
閱讀 991·2019-08-30 15:55
閱讀 3350·2019-08-30 12:50
閱讀 3495·2019-08-29 18:37
閱讀 3295·2019-08-29 16:37
閱讀 2011·2019-08-29 13:00