이 페이지는 코딩도장 데이터의 읽기 전용 정적 보관본입니다.

제곱수 계산기

어떤 자연수(n)를 제곱해서 만들어지는 수를 제곱수(n2, 완전제곱수)라고 한다. 밑(n)이 20이하인 제곱수는 아래와 같다.

1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400

자연수는 제곱수가 아닌 n의 n2k 꼴로 나타낼 수 있다. (k >= 0인 정수)

3 = 31 = 320, 4 = 22 = 221, 16 = 42 = 24 = 222, 81 = 92 = 34 = 322, 256 = 162 = 44 = 28 = 223

문제1

어떤 자연수를 입력받아서 그 수가 제곱수인지 계산하여 출력하는 프로그램을 만들어보자.

Input 예시
1
15
16
50
64
100
256
Output 예시
true
false
true
false
true
true
true

문제2

어떤 자연수를 입력받아서 제곱수가 아닌 nk 값을 계산한 후 n(k) 로 출력한다. 단, 1은 k를 0으로 출력한다

Input 예시
1
15
16
50
64
100
256
Output 예시
1(0)
15(0)
2(2)
50(0)
8(1)
10(1)
2(3)

문제3

그리고 n(k) 로 입력을 받아서 해당하는 수를 계산한다. 이때 n은 제곱수여도 상관없다. 단, n = 1은 1로 계산한다.

Input 예시
1(0)
1(16)
2(4)
3(2)
4(2)
5(0)
6(1)
Output 예시
1
1
65536
81
256
5
36

2019/08/02 16:23

AY

사실상 문제 세 개인데 하나로 써 둬서 문제를 읽다 보면 혼란스럽습니다 => 직접 수정헀음 - Noname, 2023/01/07 14:23

26개의 풀이가 있습니다.

python 3.7

def sq(num, knum = 0):
    if (num**(1/2))%1 == 0:
        return sq(int(num**(1/2)), knum+1)
    else:
        return str(num)+"("+str(knum)+")"

date = input()
if "(" in date:
    n, k = list(map(int, date[:-1].split("(")))
    print(n**(2**k))
elif int(date) == 1:
    print("1(0)")
else:
    print(sq(int(date)))

2019/08/02 17:34

AY

어떤 수가 제곱수인 경우 그 제곱근의 정수부를 제곱하면 원래 수와 같아지는 것을 제곱수 판별법으로 이용했습니다.

Python 3.7

a = input()

def int_sq_root(num): # compute integer square root if exists. OTW return 0
    b = int(num**(1/2))
    if( num - b**2 == 0):
        return b
    else: return 0


if '(' in a:  # if of the form 'a(b)', compute the number
    print( int(a[:a.find('(')])**(2**int(a[a.find('(')+1:a.find(')')])))
elif int_sq_root(int(a)) ==0 or int(a)==1: # input number is not a squre..
    print( "%s(0)" % a)
else:
    count = 1
    base = int_sq_root(int(a))
    while int_sq_root(base)!= 0:
        count+=1
        base = int_sq_root(base)
    print( "%d(%d)" %(base,count))

2019/08/05 01:17

Sechi

while True:
    user=input("Input integer or n(k): ")
    if '(' not in user:
        n=int(user)
        k=0
        if n==1:
            print("1(0)")
        else:
            while (n**0.5)%1==0:
                n=int(n**0.5)
                k+=1
            print("{}({})".format(n,k))
    else:
        user=user[:-1]
        [n,k]=[int(x) for x in user.split('(')]
        print(n**(2**k))
    print()

2019/08/12 18:04

ykleeac

import java.util.*;

public class ExponentCheck {
    public static Scanner console = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.print("Enter: "); //collects user input
        String input = console.nextLine();

        while(!input.equalsIgnoreCase("quit")) {
            try {
                int n = 0;
                int k = 1;
                if(input.contains("(")) {
                    n = Integer.parseInt(input.substring(0, 1));
                    k = Integer.parseInt(input.substring(input.indexOf("(") + 1,input.length() - 1));
                    System.out.println((int) Math.pow(n, Math.pow(2,  k)));
                } else {
                    int num = Integer.parseInt(input);  
                    if(root(num) == 0 || num == 1) {
                        n = num;
                        k = 0;
                    } else {
                        n = root(num);
                        while(root(n) != 0) {
                            k++;
                            n = root(n);
                        }
                    }
                    System.out.println(n + "(" + k + ")");
                }
            } catch(NumberFormatException nfe) {
                System.out.println("Invalid format");
            }
            System.out.println();
            System.out.print("Enter: ");
            input = console.nextLine();
        }
    }

    public static int root(int num) {
        int n = (int) Math.sqrt(num);
        if(num - Math.pow(n, 2) == 0) return n;
        else return 0;
    }
}

NumberFormatException에 대비한 try/catch 구문과 input이 quit일 때까지 출력을 반복하는 while 루프도 포함시켰습니다. 파이썬으로 작성하신 분들과 비교하니 코드가 훨씬 기네요;;

2019/08/06 13:58

박어진

Python 3.7 사용.

def Get_nk(num):
    n, k = int(num**0.5), 0
    while(num != 1 and n**2 == num):
            num, n, k = n, int(num**0.5), k+1
    return  "{}({})".format(num, k) 

def Get_num(str_nk):
    nk_lst = str_nk[:-1].split("(")
    n, k = int(nk_lst[0]), int(nk_lst[1])
    return  n**(2**k) 

usr_input=input()
result = Get_num(usr_input) if "(" in usr_input else Get_nk(int(usr_input))
print(result)

2019/08/09 09:46

왕초보

n=input("INPUT NUMBER or N(K) : ")
if "(" not in n:
    u=int(n)
    if u==1:
        print("1(0)")
    else:
        k=0
        while u**0.5==int(u**0.5):
            u=u**0.5
            k+=1
        print(f"{int(u)}({k})")
else:
    n=n[:-1]
    u,k=list(map(int,n.split("(")))
    print(u**(2**k))

2019/08/14 04:52

이우석

def sq(num, knum = 0):
    if (num**(1/2))%1 == 0:
        return sq(int(num**(1/2)), knum+1)
    else:
        return str(num)+"("+str(knum)+")"

date = input()
if "(" in date:
    n, k = list(map(int, date[:-1].split("(")))
    print(n**(2**k))
elif int(date) == 1:
    print("1(0)")
else:
    print(sq(int(date)))

2019/08/14 17:18

cond_b

javascript로 작성

getSqrt = (n, count = 0) => {
    if (typeof (n) === 'number') {
        if (n === 1) { return "1(0)" }
        if (!checkFloat(Math.sqrt(n))) {
            count++;
            return getSqrt(Math.sqrt(n), count)
        } else {
            return `${n}(${count})`
        }
    } else {
        n = n.replace(")", "")
        arr = n.split("(")
        answer = parseInt(arr[0])
        for(let i=0; i< parseInt(arr[1]); i++) {
            answer = answer*answer
        }

        return answer
    }
}

2019/08/21 22:03

임담

C#

    static void Main(string[] args)
    {
        string inp = "";
        int inp_n = 0;

        while (inp != "end")
        {
            Console.Write("자연수 입력 (종료 : end): ");
            inp = Console.ReadLine();
            if (inp == "end")
                continue;

            if(inp.Contains("(") && inp.Contains(")"))
            {
                string[] tmp;
                tmp = inp.Split(new char[] { '(', ')'});

                if (int.TryParse(tmp[0], out int tmp0) == true && int.TryParse(tmp[1], out int tmp1) == true)
                    Console.WriteLine("{0}",Math.Pow(tmp0, Math.Pow(2, tmp1)));
                else
                    Console.WriteLine("다시");

                continue;
            }
            bool gostop = int.TryParse(inp, out inp_n);
            if (gostop == false || inp_n < 1 || inp_n > 1000)
            {
                Console.WriteLine("다시");
                continue;
            }

            if (inp_n == 1)
            {
                Console.WriteLine("1 (0)");
                continue;
            }

            int n = 2, k = 0;
            while (n <= inp_n)
            {
                while (Math.Pow(n, Math.Pow(2, k)) < inp_n)
                {
                    k++;
                }
                if (Math.Pow(n, Math.Pow(2, k)) == inp_n)
                    break;
                n++;
                k = 0;
            }
            Console.WriteLine("{0} ({1})", n, k);

        }

    }

2019/08/29 15:35

류원형

function sq(x) {
    let n = x ** 0.5;
    if (Number.isInteger(n)) {
        let res = sq(n);
        res[1] += 1;
        return res;
    } else {
        return [x, 0];
    }
}

const isq = (n, k) => n**2**k;

console.log(sq(64)); // [8, 1]
console.log(isq(...sq(64))); // 64

2019/08/29 17:47

Creator

C#

자연수 NN = n**(2**k) 형태로 표현하기 위해 양변에 로그를 2회(base n, base 2) 취하여 k = log_2(log_n(N)) 값이 0 이상의 정수인 경우를 찾는 방법을 선택했습니다.

using System;
using System.Collections.Generic;
using System.Linq;

namespace CD237
{
    class Program
    {
        static void Main()
        {
            var inputCases1 = new List<int>() { 1, 15, 16, 49, 64, 100, 256 };
            foreach (int inputCase in inputCases1)
            {
                var rst = GetPowerOfNaturalNumber(inputCase);
                Console.WriteLine($"{inputCase} = {rst.Item1}({rst.Item2}), 제곱수 여부: {rst.Item3}");
            }

            var inputCases2 = new List<string>() {"1(0)", "1(16)", "2(4)", "3(2)", "4(2)", "5(0)", "6(1)" };
            foreach (var inputCase in inputCases2)
            {
                int[] separated = 
                    inputCase.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s=>int.Parse(s)).ToArray();
                Console.WriteLine($"{inputCase} -> {GetNaturalNumber(separated[0], separated[1])}");
            }
        }

        // output1 계산 - (n, k, 제곱수 여부)의 튜플 반환
        static (int, int, bool) GetPowerOfNaturalNumber(int N)
        {
            if (N == 1) { return (1, 0, true); }
            int n = 0;
            double k;
            do
            {
                k = Math.Log(Math.Log(N, ++n), 2);
            } while (k != (int)k || k < 0);
            return (n, (int)k, Math.Sqrt(N) == (int)Math.Sqrt(N));
        }

        // output2 계산
        static int GetNaturalNumber(int n, int k) => (int)Math.Pow(n, Math.Pow(2, k));
    }
}

2019/09/24 11:16

mohenjo

package d237_square_calculator;
import java.util.Scanner;
public class SquareCalculator {

    long square_find(long input) {
        long i=1;
        for(i=1; i<input; i++) {
            if(i*i==input) break;
        }
        return i; //제곱수가 아닐 경우에는 input을 그대로 반환하게 된다.
    }

    public static void main(String[] args) {
        SquareCalculator sq = new SquareCalculator();
        Scanner sc = new Scanner(System.in);
        long input=1, output;
        int i;

        while(input!=0) {
            i=0;
            input = sc.nextLong();
            output = input;
            while (output!=sq.square_find(output)) {
                output=sq.square_find(output);
                i++;
            }
            System.out.println(output + "("+i+")");
        }
    }
}
1
1(0)
15
15(0)
16
2(2)
49
7(1)
64
8(1)
100
10(1)
256
2(3)

package d237_square_calculator;
import java.util.Scanner;
public class SquareCalcul2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long input=1;
        long square_n;

        while(input!=0) {
            input = sc.nextLong();
            square_n = sc.nextLong();

            while(square_n!=0) {
                input = input*input;
                square_n--;
            }
            System.out.println(input);
        }
    }
}

1 0
1
1 16
1
2 4
65536
3 2
81
4 2
256
5 0
5
6 1
36

2019/10/17 16:26

Katherine

while True:
 num = int(input('수를 입력하시오.'))
 print(num**2)

파이썬으로 했고 영구적입니다.

2019/11/01 18:33

박지훈

from math import sqrt


# n(k) 출력
n = int(input())
k = 0

while 1:
    if sqrt(n) % 1 == 0:
        k += 1
        n = sqrt(n)
    else:
        break


print('%d(%d)' % (n, k))


# n(k)입력 받는 함수
temp = input()
n = int(temp[0])
k = int(temp[2:int(len(temp))-1])

print(n**2**k)


2019/12/30 14:49

농창

n=input("숫자를 입력하거나 n(k)를 입력하십시오: ")
if "(" in n:
    n=n[:-1]
    n=n.split("(")
    result=int(n[0])**2**int(n[1])
    print(result)
else:
    n=int(n)
    k=0
    if n==1:
        print("1(0)")
    else:
        while (n**(1/2))%1==0:
            n=int(n**(1/2))
            k+=1
        print("%d(%d)" %(n,k))

2020/01/01 17:15

박시원

def square(N):
    check = 0
    n = N
    while(True):
        if n**0.5 - int(n**0.5) == 0:
            check += 1
            n = n**0.5
        elif n**0.5 - int(n**0.5) != 1:
            return str(int(n))+"("+str(int(check))+")"

def make(N):
    n = ""
    checklist1 = list()
    checklist2 = list()
    check = 0
    mainlist = list(N)
    for i in range(len(mainlist)):
        if mainlist[i] == '(':
            check = 1
        if check == 0:
            checklist1.append(mainlist[i])
        elif check == 1:
            checklist2.append(mainlist[i])
    checklist2.pop(0)
    checklist2.pop(len(checklist2)-1)
    for j in range(len(checklist1)):
        n += checklist1[j]
    check = ""
    for k in range(len(checklist2)):
        check += checklist2[k]
    n = int(n)
    check = int(check)
    for i in range(check):
        n *= n
    return n

N =input()
try:
    N = int(N)
    print(square(N))
except ValueError:
    print(make(N))


2020/03/11 17:34

BlakeLee

num=int(input('Input  a number...'))
n,k=num,0

while (n !=1 and (int(n**0.5) == n**0.5)):
    n,k=int(n**0.5), k+1

print(n,'(',k,')')

2020/03/30 12:53

Buckshot

<결과> Input a number...1 1 ( 0 ) Input a number...15 15 ( 0 ) Input a number...16 2 ( 2 ) Input a number...49 7 ( 1 ) Input a number...64 8 ( 1 ) Input a number...100 10 ( 1 ) Input a number...256 2 ( 3 ) - Buckshot, 2020/03/30 12:53
import re
def sqn(num1, k):   # n(k)로 자연수를 변환하는 함수
    if num1 != (num1**.5) * (num1**.5):
        print('{}({})'.format(int(num1), k))
        return k, num1
    else:
        k += 1
        sqn((num1**.5), k)

def t_sqn(num2):    # n(k) 형태를 자연수로 역변환하는 함수
    print('{}'.format(t_in2[0]**(2**t_in2[1])))

if __name__ == '__main__':
    in1, k = 256, 0
    sqn(in1, k)
    in2 = '4(2)'
    t_in2 = list(map(int, re.findall('[0-9]', in2)))
    t_sqn(t_in2)

2020/05/24 10:29

Hwaseong Nam

#inputA
import math
def find(a):
    if math.sqrt(a) != int(math.sqrt(a)):
        return print(a,'(0)')
    elif a ==1:
        return print(a,'(0)')
    else:
        count = 0
        while math.sqrt(a) == int(math.sqrt(a)):
            a = math.sqrt(a)
            count += 1    
    return print(int(a),count)

#inputB
def rev_find(a,b):
    return a**2**b

2020/07/05 15:27

Money_Coding

import math

def sol(inp) :
    counter = 0
    while True :
        sq = math.sqrt(inp)
        if sq == int(sq) and sq != 1 :
            counter += 1
            inp = sq
        else :
            return "%s(%s)"%(inp, counter)

if __name__ == "__main__" :
    print(sol(1))
    print(sol(15))
    print(sol(16))
    print(sol(49))
    print(sol(64))
    print(sol(100))
    print(sol(256))

결과

1(0)
15(0)
2.0(2)
7.0(1)
8.0(1)
10.0(1)
2.0(3)

2020/08/18 16:57

GG

import math
temp = 256
n = 0
while 1:
    if pow(int(math.sqrt(temp)), 2) == temp:
        temp = int(math.sqrt(temp))
        n += 1
    else:
        break
print('{}({})'.format(temp, n))

2021/02/03 19:19

김민수

from math import *

#1번

n = int(input("자연수 n을 입력하시오"))

def fun_k (i):
    k=0
    a=i
    while a%2==0:
        a//=2
        k+=1
    return k

def convert(i):
    a=i
    k=0
    if not sqrt(a).is_integer():
        return i,k
    else:
        while sqrt(a).is_integer():
            if a == sqrt(a):
                break
            elif sqrt(a).is_integer():
                a=sqrt(a)
                k+=1
        return int(a),k

print(convert(n))

# 2번
a = input("n(k)를 입력하시오").rstrip(')').split('(')

def convert1 (a):
    n =int(a[0])
    k =int(a[1])
    return n**(2**k)

print(convert1(a))

2022/01/22 21:20

양캠부부

dart

import 'dart:math';

bool isInt(num x) => x is int || x == x.truncateToDouble();
bool isSquareNumber(int x) => isInt(sqrt(x));

List<int> toTwosSquare(int x) {  
  int k = 0;
  while (x != 1) {
    double x_sqrt = sqrt(x);
    if (!isInt(x_sqrt)) break;
    k++;
    x = x_sqrt.truncate();
  }  
  return [x, k];
}

void main() {
  List data1 = [1, 15, 16, 50, 64, 100, 256];
  List data3 = [[1, 0], [1, 16], [2, 4], [3, 2], [4, 2], [5, 0], [6, 1]];  

  for (int x in data1) {    
    List res = toTwosSquare(x);
    print('$x: ${isSquareNumber(x)}, ${toTwosSquare(x)}');
  }

  for (List<int> list in data3) {
    int n = list[0], k = list[1];
    print("$n($k): ${pow(n, pow(2, k))}");
  }
}

2023/01/07 15:11

Noname

import math

def get_num():
    while (1):
        input_num = input("자연수 입력 : ")
        try:
            input_num = int(input_num)
            break
        except:
            print("잘못된 입력입니다.")
    return input_num

def ex_1():
    input_num = get_num()

    if math.sqrt(input_num).is_integer():
        print("True")
    else:
        print("False")


def ex_2():
    input_num = get_num()
    val_dic = {}
    i = 2

    while input_num != 1:
        if input_num % i == 0:
            if i not in val_dic:
                val_dic[i] = -1
            val_dic[i] += 1
            input_num /= i
        else:
            i += 1

    for i, j in val_dic.items():
        print(f"{i}({j})")

def ex_3():
    while(True):
        input_value = input("입력 : ")
        try:
            spl_value = input_value.split("(")
            input_num = int(spl_value[0])
            range_num = int(spl_value[1][:-1])
            break
        except ValueError:
            print("잘못된 입력입니다.")

    result = input_num ** (range_num + 1)
    print(result)

2023/04/05 13:28

HoHyeon Kim

using System;

namespace solution
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("제곱수인지 자연수를 입력해 보세요: ");
            int su = int.Parse(Console.ReadLine());
            Console.WriteLine(isSquareNumber(su));
            Console.WriteLine("{0} :  {1}", su, calNK(su));

            Console.Write("\n  n(k) 로 입력하세요: ");
            string nk = Console.ReadLine();
            Console.WriteLine("{0} :  {1}", nk, calcNK(nk));
        }        

        private static bool isSquareNumber(int su)
        {
            int n = (int)Math.Sqrt(su);
            return n * n == su;
        }

        private static string calNK(int su)
        {
            if (su == 1)
                return "1(0)";
            int k = 0;
            while (isSquareNumber(su))
            {
                k++;
                su = (int)Math.Sqrt(su);
            }
            return su + "(" + k + ")";
        }

        private static int calcNK(string nk)
        {
            int n = 0, k = 0;
            string str = "";
            for (int i = 0; i < nk.Length; i++)
            {
                if (nk[i] == '(')
                {
                    n = int.Parse(str);
                    str = "";
                }
                else if (nk[i] == ')')
                    k = int.Parse(str);
                else
                    str += nk[i];
            }

            return (int)Math.Pow(n, Math.Pow(2, k));
        }
    }
}

2023/07/29 22:55

insperChoi

python 3.8

# 문제1의 해
from math import sqrt

inp = """1
15
16
50
64
100
256"""

any_n =[int(n) for n in inp.split("\n")]
for n in any_n:
    if sqrt(n) == int(sqrt(n)):
        print(True)
    else:
        print(False)

# 문제2의 해
def NonSquareNum(n):
    nsn = [i for i in range(1, n+1) if sqrt(i) != int(sqrt(i)) ]
    return nsn

for i in any_n:
    if not NonSquareNum(i) :
        print(f"{i}(0)")
    for n in NonSquareNum(i):
        for k in range(10): # range(10)에서 10은 입력된 정수보다는 작은 임의의 값.
            if n**(2**k) == i:
                print(f"{n}({k})")

# 문제2의 다른 해
from math import log2, log
for i in any_n:
    if not NonSquareNum(i) :
        print(f"{i} : {i}(0)")
    for n in NonSquareNum(i):
        result = log(i) / log(n)
        k = log2(result)
        if k == int(k):
            print(f"{i} : {n}({int(k)})")


# 문제3의 해
import re

inp_v = """1(0)
1(16)
2(4)
3(2)
4(2)
5(0)
6(1)"""

any_inp =[v for v in inp_v.split("\n")]
print(any_inp)
for v in any_inp:
    n, k = map(int, re.findall(r'\d+', v))
    print(f"{v} = {n**(2**k)}")

2024/04/14 11:53

김맹준

목록으로