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

Smallest multiple

출처 : 프로젝트 오일러 5번문제, 한글 번역판


1 ~ 10 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수는 2520입니다.

그러면 1 ~ 20 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수는 얼마입니까?

2017/06/04 23:58

S ReolSt

72개의 풀이가 있습니다.

python 3.6, 재귀호출 & functools.reduce 1부터 20까지. 예를 들면, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 이것은 ((((1+2)+3)+4)+5) 됩니다.

import functools

def gcd(x, y):
    if y: return gcd(y, x%y)
    else: return x

def lcm(x, y):
    return int(x * y / gcd(x, y))

>>> print(functools.reduce(lambda x, y: lcm(x, y), [a for a in range(1,21)]))
232792560

2017/06/06 16:51

예강효빠

배우고 갑니다 ^^ - Taesoo Kim, 2018/06/15 15:19

Ruby

lcm = ->a,b { a*b / (a,b = b,a%b until b == 0; a) }
sm = ->range { range.reduce &lcm }

Test

expect( sm[1..10] ).to eq 2520
expect( sm[1..20] ).to eq 232792560
expect( sm[1..20] ).to eq (1..20).reduce(:lcm) # same to stdlib's lcm.

2017/06/05 18:25

rk

Python3 입니다. 답은 232792560 이네요.

smallestMultiple = 2
increment = 2
count = 3

while count <= 20:
    if smallestMultiple % count != 0:
        smallestMultiple = smallestMultiple + increment
    else:
        count = count + 1
        increment = smallestMultiple

2017/06/17 23:15

SPJung

일단 예를 들어, 가장 큰 20부터 차례대로(하차열) 그 수의 약수들을 제외시킵니다. 이렇게 1까지 과정을 반복하면 수들의 리스트에는 20과 같은 큰 수들과 일부 소수가 남게 됩니다. 이때 남은 수들을 모두 곱해주면 답이 나옵니다.

2017/07/31 18:45

P.Y.Thon

예강효빠님의 풀이를 참고하여 1 부터 n까지의 수들의 최소공배수가 답임을 알았습니다.

from math import gcd

def lcm(a,b):
    return int(a*b/gcd(a,b))

def small(n):
    n = list(range(1,n+1))
    while len(n) != 1:
        n[0:2] = [lcm(n[0],n[1])]
    return n[0]

2019/02/15 13:20

김영성

최소공배수를 구하기 위하여 온갖 별의 별 행동을 하던 차, math모듈의 존재를 잊어버리고 있었읍니다;; 확실히 코드가 간결해 지는군요! - 암살자까마귀, 2019/05/08 23:36
i, sig = 1, 0
while True : 
    sig = 0
    for k in range(1, 21) :
        if i%k != 0 :
            sig = 1
            break
    if sig == 0 :
        print(i)
        break
    i += 1

주먹구구식으로 했더니 한세월 걸리네요ㅜㅜ

결과

232792560

2019/11/08 17:30

GG

result=1
i=0
j=1
number=range(1,21)
numbers=[0]*len(number)
ref = [0]*20

while True:
    for x in number:
        numbers[x-1] = result % x
    if numbers == ref:
        print(result)
        break
    else:
        print(result)
        j=j+1
        result=20*j

1~20까지 나눈 값이 모두 0인 배열을 찾는다. 답은 232792560

2017/06/08 02:35

hanny


void multi(vector<int>&vt, int num)
{
    for (int iter = 0; iter < vt.size(); iter++)
    {
        if ((num%vt[iter] )== 0)
        {
            num /= vt[iter];
        }
    }
    int mod = 2;
    while (mod <= num)
    {
        if ((num%mod )== 0)
        {
            vt.push_back(mod);
            num /= mod;
        }
        else
        {
            mod++;
        }
    }
}
void CsmallDlg::OnBnClickedOk()
{
    vector<int> vt;
    for (int iter = 2; iter <= 20; iter++)
    {
        multi(vt, iter);
    }
    int nResult = 1;
    CString str;
    for (int iter = 0; iter < vt.size(); iter++)
    {
        nResult *= vt[iter];
        str.AppendFormat(_T("%d\n"), vt[iter]);
    }
    str.AppendFormat(_T("%d"),nResult);
    AfxMessageBox(str);
}

2017/06/09 15:57

김 영남

var gcd = function g(x, y) {
    return y ? g(y, x % y) : x;
}

var lcd = function(x, y) {
    return x * y / gcd(x, y);
}

var arr = Array.from({length: 20}, (v, k) => k + 1); // make 1 to 20 array with array-like object and mapFn

var res = arr.reduce(lcd);

console.log(res);

2017/06/13 16:19

funnystyle

// 20까지 모두 나누어 떨어진다 - C
#include <stdio.h>
int main(void)
{
    int i, j, c;
    for (i = 1; ; i++)
    {
        c = 0;
        for (j = 1; j <= 20; j++)
        {
            if (i % j != 0)
            {
                c = 1; break;
            }
        }
        if (c == 0)
        {
            printf("%d", i);
            // 답은 232792560
            break;
        }
    }
}

2017/06/14 11:42

Jeong Hoon Lee

  1. 구할려는 수는 2520에 11부터 20까지 있는 소수를 곱한 값보다는 클거라 예상
  2. 1~10까지의 수는 11부터 20까지의 수의 약수가 될수 있으므로 구할려는 수를 11~20까지로만 나눠서 나머지를 확인함
public class Multiple {
    public static void main(String[] args) {
        int count = 2520*11*13*17*19;
        int success = 0;
        while(true) {
            count++;
            for(int i=11;i<21;i++) {
                if(count%i == 0) {
                    success++;
                }
            }
            if(success == 10) {
                System.out.println(count);
                break;
            }
            success = 0;

        }
    }

}

출력 : 232792560

처음 count값에다가 그냥 2 곱하니까 출력값이 나오네요 11은 만들어지고 12는 3*4로 만들어지고 계속해보면 16이 안만들어지는데 16만들기 위해서 2가 필요해서 2만 곱하면 되는것 같아요

2017/06/18 09:31

JH

파이썬 3.6으로 했습니다.

#codingdojang.com 132번 문제.
primenumber = [2]
interval = 20; temp = 3

#20까지의 소수를 구합니다.
while(temp < interval) : 
    if temp % 2 == 0 : #2의 배수는 생각할 필요도 없이 pass
        temp = temp + 1
        continue
    for i in primenumber : #list내의 소수와 비교해서 나누어 떨어진다면 not 소수
        if temp % i == 0 :
            break
        elif i == primenumber[len(primenumber) - 1]:
            primenumber.append(temp)
    temp = temp + 1


#소수별로 20을 넘지않는 제곱수를 구합니다.
#예를 들어서 2는 16, 3은 9, 5는 5, ... 이런식으로 다 구해서
#곱한 수가 1~20사이의 어떤 수로도 나누어 떨어지는 가장 작은 수입니다.

result = 1
for i in primenumber :
    while(1) :
        if i * i < interval :
            i = i * i
        else :
            result = result * i
            break
print(result)

2017/06/26 20:20

KimSeonbin

파이썬 3.6입니다

i=2*3*5*7*11*13*17*19
k=1

while True:
    if i%k==0 and k<20:
        k=k+1

    elif i%k !=0:
        i=i+2*3*5*7*11*13*17*19
        k=1

    elif i%k==0 and k==20:
        print(i)
        break

2017/07/01 11:18

안성준

limit=20
i=0
f=True
while f:
    i=i+1
    for d in range(1, limit+1):
        if i%d!=0:
            break
        if d==limit and i%d==0:
            f=False
print(i)

2017/07/01 17:50

keroro10398

1에서 20사이어 어떤 수로든 나누어 떨어지는 수는 1과 20사이의 모든 자연수의 공배수이고, 그 중 가장 작은 수는 최소공배수입니다.

세 자연수 A, B, C의 최소 공배수는 A, B의 최소공배수 L 과 나머지 C의 최소 공배수와 같습니다.

따라서 최소공배수를 구하는 함수를 만들고, 1,2,...20 의 리스트를 이 함수를 적용해서 접으면 됩니다.

Swift 입니다.

func gcd(_ m: Int, _ n: Int) -> Int {
    guard m > 0, n > 0 else { return 0 }
    guard m >= n else { return gcd(n, m) }

    if m % n == 0 { return n }
    return gcd(n, m % n)
}

func lcm(_ m: Int, _ n: Int) -> Int {
    guard case let g = gcd(m, n), g > 0 else { return 0 }
    let (a, b) = (m / g, n / g)
    return a * b * g
}

print(Array(1...20).reduce(1, lcm))

2017/07/06 14:42

룰루랄라

num = 2520
for x in range(11,21): 
    if num % x ==0:
        continue
    elif num % x != 0:
        for y in range(2,10):
            if (num*y) % x ==0:
                num = num * y
                break
        else: num = num * x
print(num)

아직초보라 잘 하지는 못합니다...

2017/07/15 17:25

이기현

object Problem5 {
  def IsCorrect(N : Int, i : Int, Ei : Int): Int = if(N % i == 0) {if(i > Ei) IsCorrect(N,i - 1,Ei) else 1} else 0
  def IsPrime(N : Int, i : Int): Int = if(N % i != 0) {if(i > 2) IsPrime(N,i - 1) else 1} else if(N == 2 || N == 3) 1 else 0
  def Multiple(ML : IndexedSeq[Int], ind : Int, N : Int) : Int = if(ind < ML.length - 1) Multiple(ML, ind + 1, N * ML(ind)) else  N * ML(ind)
  def Search(N : Int, i : Int): Int = if(IsCorrect(N * i,20,11) == 1) N * i else Search(N, i + 1)
  def main(args: Array[String]): Unit = print(Search(Multiple((2 to 20) filter(x => IsPrime(x,x/2) == 1),0,1),2))
}

//부작용 없는 순수한 함수형 프로그래밍에 가까워지도록 노력해보았습니다. 재귀를 많이 쓰긴 했지만 전부 꼬리재귀라 stack overflow 걱정은 없습니다.
(수정) scala 코드는 예전에 쓴 것입니다.

(defn IsCorrect [n] (if(= (count (for [x (range 11 20) :when (= (mod n x) 0)] x)) 9) 1 0))
(defn IsPrime [n] (if(or (= (count (for [x (range 2 (/ n 2)) :when (= (mod n x) 0)] x)) 0) (= n (or 2 3))) 1 0))
(defn Search [n i] (if(= (IsCorrect (* n i)) 1) (* n i) (Search n (inc i))))
(println (Search (reduce * (for [x (range 2 20) :when (= (IsPrime x) 1)] x)), 2))

조금 더 익숙해진 뒤에 Clojure로도 작성해 보았습니다.

2017/07/18 00:44

S ReolSt

Smallest multiple Number for 20 is : 232792560

public class Calculate {

    public static void main(String[] args) {
        int maxInt = 20;
        boolean found = false;

        for(int sm=1 ; ; sm++) { //최소공배수로 확인할 수
            for( int j=1 ; j<=maxInt ; j++) { // 나누는 수
                if(sm%j != 0) break; // 나누는 수로 나누어지지 않으면 
                if( j==maxInt ) { // 나누는 수가 max 값이면 출력 후 종료
                    System.out.println("Smallest multiple Number for " + maxInt + " is : " + sm);
                    found = true;
                }
            }
            if(found) break;
        }
    }
}

2017/07/21 16:33

SH

# Python 3.6
x = 20
while True:
    rtn = list(filter(lambda y: x % y, range(2, 21)))

    if len(rtn) == 0:
        print("The number is %s" % x);    #The number is 232792560
        break
    x += 1

2017/07/23 04:20

최우인

from math import gcd
from functools import reduce

print(reduce(lambda x,y: x*y//gcd(x,y), range(1, 21))) # 232792560

2017/08/04 05:51

Noname

# python 3.6
n = nmax = 20
while True:
    chk = any([n % i for i in range(1, nmax + 1)])
    if chk is False:
        print(n)
        break
    n += nmax
# ans: 232792560

2017/09/05 12:55

mohenjo

def smallest_multiple(min, max) :
    the_range = range(min, max + 1)
    n = 1

    while True :
        box = []
        for i in the_range :
            if n % i == 0 :
                box.append(1)
            else :
                pass
        if len(box) == len(the_range) :
            break
        else :
            n += 1

    return n

print(smallest_multiple(1, 20))

2017/09/12 12:42

다크엔젤

namespace _20170916
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
             RE:
            for(int i=1;i<=20;i++)
            {
                int b = a % i;
                if(b!=0)
                {
                    int c = i - b;
                    a = a + c;
                    goto RE;
                }

            }
            Console.WriteLine(a);
        }
    }
}

2017/09/16 12:36

정주영

다른 2 레벨 문제들에 비해 구현하는 것은 너무 쉬운 편인 것 같아서, 가능한 실행 속도를 최대한 개선시키는 것이 관건이라고 생각했습니다. Break 문을 사용해서 1 ~ 20으로 나눠보는 도중 단 한 번이라도 나눠지지 않는 경우 바로 다음 수로 넘어가게 했습니다. 제 능력선에서 가능한 최대한 실행 속도를 개선하려고 해봤는데 이게 최선인지는 모르겠네요.

public class SmallestMultiple {
    public static void main(String[] args) {
        for(int num = 1; num < Integer.MAX_VALUE; num++) {
            int count = 0;
            for(int divideNum = 1; divideNum <= 20; divideNum++) {
                if(num % divideNum == 0) {
                    count++;
                } else {
                    break;
                }
            }
            if(count == 20) {
                System.out.println(num);
                break;
            }
        }
    }
}

2017/10/25 05:50

SungWook Jung

gcd = lambda a, b : gcd(b, a%b) if a%b else b
lcm = lambda a, b : a * b // gcd(a, b)


res = 1
for i in range(2, 21):
    res = lcm(i, res)
print(res)

2017/11/10 10:07

songci

파이썬 3.6

def main(n):
    num = 1
    while True:
        for i in range(1,n+1):
            if num % i != 0: break
        if i == n:
            print(num)
            break
        else: num += 1

if __name__ == "__main__":
    n = int(input("범위 마지막 수를 입력하세요 = "))
    main(n)

*결과값
범위 마지막 수를 입력하세요 = 20
232792560

2018/01/21 12:56

justbegin

Java 입니다.

public class level_2_smallest_mulitple {

public static void main(String[] args) {

    for(int i = 1; i <= Integer.MAX_VALUE; i++)
    {
        int count = 0;          
        for(int j = 1; j <= 20; j++)
        {
            if(i % j == 0)
            {
                count++;
            }
            else
            {
                break;
            }
        }
        if(count == 20)
        {
            System.out.println(i);
            break;
        }
    }
}

}

2018/01/23 11:12

Byam_Gyu

# 파이썬

# '에라토스테네스의 체'로 소수를 구한다.
# n이하 소수를 거듭제곱하여 n이하 최대로 올린다.
# 거듭제곱 하지 않은 소수들과 거듭제곱 수들을 곱한다.


def eratosthenes(i):
    candidates = list(range(3, i+1))
    base = 2
    product = 2
    prime = [2]
    while candidates:
        while product <= i:
            product += base
            if product in candidates:
                candidates.remove(product)
        base = candidates.pop(0)
        product = base
        prime.append(base)
    return prime


def smallest_multiple(i):
    primes = eratosthenes(i)
    print(primes)
    for m in range(len(primes)):
        prime = primes[m]
        while primes[m]*prime < i:
            primes[m] *= prime
    print(primes)
    r = 1
    for n in primes:
        r *= n
    return r


print(smallest_multiple(20))


2018/02/02 01:46

olclocr

def primeset(n):
    l = list()
    for i in range(2, n + 1):
        if all([i % j for j in l]):
            l.append(i)
    return l
def smallmul(n):
    result = 1
    for i in primeset(n):
        j = 0
        while i**j <= n:
            j += 1
        result = result * (i**(j-1))
    return result


print(smallmul(20))

2018/02/14 14:01

김동하

파이썬3.6

import math
from functools import reduce

def lcm(*numbers):
    def lcm(a, b):
        return (a * b) // math.gcd(a, b)
    return reduce(lcm, numbers)

assert lcm(*range(1, 10 + 1)) == 2520
print(lcm(*range(1, 20 + 1))) #232792560

2018/04/05 14:32

디디

Swift입니다.

단순하게, 20부터 20씩 증가하면서 1부터 20까지 나눠 떨어지는지를 검사합니다.

import Foundation

var dividers = Array(1...20)

func canBeDividedByAll(_ number : Int) -> Bool {
    for divider in dividers {
        if number % divider != 0 {
            return false
        }
    }
    return true
}

func findMinNumber() -> Int {
    var number = 20
    while true {
        if canBeDividedByAll(number) {
            break
        }
        number += 20
    }
    return number
}

print( findMinNumber() )

2018/04/12 10:09

졸린하마

// 자바
public static void main(String[] args) throws Exception {
//      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//      int n = Integer.parseInt(br.readLine());

        boolean flag = false;
        long n = 2519;
        while(!flag) {
            n++;
            flag = true;
            for (long i=1; i<20; i++) {
                if (n%i != 0)
                    flag = false;
            }
            System.out.println(n);
        }

    }//232792560 답나오긴 하는데 개오래 걸림... 쓰레기 코드입니다

2018/05/07 01:17

정몽준

public class test {
    public static void main(String[] args) {
        for(int num = 1; num < Integer.MAX_VALUE; num++) {
            int count = 0;
            for(int a = 1; a <= 20; a++) {
                if(num % a == 0) {
                    count++;
                } else {
                    break;
                }
            }
            if(count == 20) {
                System.out.println(num);
                break;
            }
        }
    }
}

2018/06/04 12:23

聂金鹏

Python 다른사람 참고

import functools


# 최대공약수
def gcd(x, y):
    if y:
        return gcd(y, x % y)
    else:
        return x


# 최소공배수
def lcm(x, y):
    return int(x*y/gcd(x, y))


print(functools.reduce(lambda x, y: lcm(x, y), [a for a in range(11, 21)]))

2018/06/15 15:19

Taesoo Kim

from functools import reduce

def lcm(a,b):
    c = a*b
    while b > 0: a,b = b,a%b
    return c/a

print(reduce(lcm,range(1,21)))

2018/07/24 22:48

Creator

#include <stdio.h>

#define NUM 20

int main(void)
{
    int num=10, j, boolean;

    while(1)
    {
        boolean = 0;         

        for(j=2; j<=NUM; j++) //1로 는 어떤수도나누어 떨어지기떄문에 2이상 
        {
            if(num%j!=0)
            {
                boolean =1;
                break;        
            }
        }

        if(boolean == 0)
        {
            printf("%d", num);
            break;
        }

        num++;
    }
}

2018/08/15 12:48

이우경

Matrix = [[0]*8 for i in range(21)]  # 8*21 matrix (마지막 줄은 출력값)
lst_prime = [2, 3, 5, 7, 11, 13, 17, 19]

result = 1
i = 1
while i <= 20:
    for k in lst_prime:
        e = 0
        while i % (k ** e) == 0:
            e = e + 1
        e = e - 1
        j = lst_prime.index(k)
        Matrix[i-1][j] = e
        if e >= Matrix[20][j]:
            Matrix[20][j] = e
    i = i + 1

for k in lst_prime:
    j = lst_prime.index(k)
    result = result * (k ** Matrix[20][j])

print(result)

저는 python을 사용하였습니다. 1~20을 행에, 20이하의 소수를 열에 배치하고 각 칸에는 행 값의 지수를 적습니다. 그리고 각 열의 최댓값을 마지막 행에 적어두어 이에 해당하는 수를 취하면 됩니다. 최댓값을 취할 때에는 각 행에 대해서 마지막 행과의 비교 연산을 수행하였습니다. 보시다시피 for문을 2번이나 사용하였는데, while문을 수행함과 동시에 result 값을 올리려면 개행하는 타이밍이 없기에 중복하여 사용하였습니다.

2018/08/23 01:41

aa

import functools

def func(x,y):
    if y:
        return func(y, x%y)
    else:
        return x

print(functools.reduce(lambda x,y: x*y//func(x,y), [a for a in range(1,21)]))

2018/08/24 13:21

S.H

int i = 7 * 11 * 13 * 15 * 17 * 19 * 20;
        boolean go = false;
        while (!go) {
            go = true;
            for (int j = 11; j < 21; j++) {
                if (i++ % j != 0) {
                    go = false;
                    break;
                }
            }
        }
        System.out.println(i);

2018/08/26 20:24

김지훈

boolean ha = true;
        for (int j = 1; ; j++) {
            ha = true;
            for (int i = 1; i <= 20; i++) {
                if (j % i != 0) {
                    ha = false;
                    break;
                }
            }
            if(ha == true) {
                System.out.println(j);
                break;
            }
        }

2018/08/26 20:35

채규빈

#Greatest Common Divisor(최대공약수)
def gcd(a,b) :
    gcd_num = min(a,b)+1
    while gcd_num!= 1:
        if a%gcd_num==0 and b%gcd_num==0:
            return gcd_num
        gcd_num= gcd_num-1
    return 1

#Least Common Multiple(최소공배수)
#어떤 수로도 나누어 떨어지는 가장 작은 수 = 최소공배수
#최대공약수와 최대공배수의 관계 (두 자연수 A, B) 
#A*B=gcd*lcm
def lcm(n):
    lcm_num=1
    for i in range(2, n):
        lcm_num=int((lcm_num*i)/gcd(lcm_num,i))        
    return lcm_num

2018/11/05 22:57

쨔이

package pac;
import java.util.*;

public class franklin {
    public static void main(String[] args)
    {
        int i=0,j=0,k=0;
        while(true)
        {
            i++;
            for(j=1;j<21;j++)
            {
                if(i%j==0)
                {
                    k++;
                }

            }
            if(k == 20)
                break;
            else
                k = 0;
        }
        System.out.println(i);
    }
}

2018/12/24 14:21

dbnfqe

from math import gcd

def lcm(a, b):
    Lcm = a * b / gcd(a, b)
    return int(Lcm)

num_list = [i for i in range(2, 20)]

for i in range(17):
    num_list[i+1] = lcm(num_list[i],num_list[i+1])

print(num_list[17])


2019/02/07 15:52

D.H.

prime_number =[]
for i in range(2,21):
    chk = True
    for j in range(2,i):
        if i%j == 0:
            chk = False
            break
    if chk:
        prime_number .append(i)
multiplication=1
for z in prime_number:
    multiplication*=int(z)

answer=0
while True:
    ti=0
    answer = answer + multiplication
    for i in range(1,21):
        i=int(i)
        if answer%i==0:
            ti+=1
        if ti==20:
            print(answer)
            quit()

2019/02/15 17:02

이정헌

static void Main(string[] args)
        {
            Console.WriteLine("*** 코딩도장 Q132 ***");

            int startNum = 1;
            int endNum = 20;
            int ans = 1;

            for (int i = startNum; i < endNum; i++)
            {
                if (ans%i!=0)
                {
                    ans++;
                    i = startNum;
                    continue;
                }
            }
            Console.WriteLine("정답은 {0} 입니다.", ans);

2019/02/24 18:41

DrKilling

namespace codingdojang__
{
    class Program
    {
        static void Main(string[] args)
        {
            bool found = false;
            int number = 20;
            int temp = 0;

            while (found == false)
            {
                for (int i = 11; i < 21; i++)
                {
                    if (number % i != 0)
                    {
                        break;
                    }
                    else if (i == 20 && number % i == 0)
                    {
                        found = true;
                        temp = number;
                    }
                }
                number += 20;
            }
            Console.WriteLine(temp);
        }
    }
}

2019/02/26 17:28

bat

num=2
sum=0
while True:
    for div in range(1,21):
        if num%div!=0:
            num += 2
            break
        elif div==20:
            print(num)
            exit()

2019/02/28 12:06

def factor(n):
    factor=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            factor.append(i)
            if n/i!=i:
                factor.append(int(n/i))
    return factor

def lcm(a,b):
    fa=factor(a)
    fb=factor(b)
    fab=[]
    for i in fa:
        if i in fb:
            fab.append(i)
    return a*b//max(fab)

mul=1
for i in range(1,21):
    mul=lcm(mul,i)

print(mul)


2019/03/02 20:03

ykleeac

자바입니다. 생각보다 간단하네요.


public class practice {

    public static void main(String[] args) {

        int n = 1;

        while(true) {
            int cnt= 0;
        for (int i = 1; i <= 20; i++)
        {
            if (n % i ==0)
                {cnt++;}
        }
        if (cnt == 20)
            {System.out.println(n);
        break;}
        else {
            n++;
        }
        }

    }
}

2019/04/02 14:52

Albert

def sosu(LIST,ALIST):
    if LIST==[]:
        return ALIST

    for i in range(2,LIST[0]): 
        if LIST[0]%i==0:
            del LIST[0]
            return sosu(LIST,ALIST)

    ALIST.append(LIST[0]);del LIST[0]
    return sosu(LIST,ALIST)

ANSL=sosu(list(range(3,21)),[2]);ansl=sosu(list(range(3,21)),[2]);ans=1

for i in range(len(ANSL)):
    while 1:
        if ANSL[i]*ansl[i]>20:
            break
        ANSL[i]*=ansl[i]
    ans*=ANSL[i]

print(ans)

일전에 레벨1에서 풀어본 적이 있는 문제같은데, 잘 기억이 나질 않는군요. 20이하의 소수를 죄다 구하여 그들의 20 이하의 최대 제곱수들을 곱하였읍니다.그래도 상당히 매우 길으군요 T.T

from math import*

def lcm(q,w):
    return int(q*w/gcd(q,w))

def ans(q,w,N):
    if q==N:
        return lcm(q,w)
    return ans(q+1,lcm(q,w),N)

print(ans(1,2,int(input())))

math모듈을 사용하여 다시 풀어보았읍니다. 역시 최대공약수와 최소공배수를 단번에 구할수있어서 확실히 코드의 길이가 줄어들었읍니다. 모처럼이니, 굳이 상한을 20이 아닌 사용자가 직접 설정 할 수 있도록 하였읍니다. 그러나 저의 파이손으로는 상한 216까지만 계산이 되고(대략 10의 307제곱 자리 크기이군요.) 이 보다 큰 수라면 계산용량을 초과해 버리는군요.^^

2019/05/08 23:02

암살자까마귀

11부터 20 까지의 최소공배수를 구하면 됩니다.
2의 지수가 있는수 - 12,14,16,18,20. 최대 - 16.(2^4)
3 - 12,15,18 최대 - 18.(3^2)
5 - 10,15,20. 최대 - 다.(5^1)
5 이후는 모두 지수가 1이니,
2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19 를 하면 됩니다.
--- 232,792,560
  • Made by Endever

2019/05/21 21:46

Firelight

자연수 X,Y가 있을 경우 최소공배수는 (X*Y/최대공약수)라고 하더라고요..... 두수의 최소공배수를 구하고 구한 최소공배수를 다음수랑 비교하는 방식으로 구했습니다. 처음 최소공배수를 6으로 설정한 이유는 2와 3의 최소공배수가 6이기 때문입니다. 최소공배수가 다음수보다 크다는 설정을 했습니다.

public class SmallestMultiple {

    public static void main(String[] args) {

        long 최소공배수 = 6;  //최소공배수
        for(int i=4; i<21; i++) {
            int 최대공약수 = 0;  //최대공약수
            for(int j=2;j<i+1;j++) {
                if(최소공배수%j==0&&i%j==0) {
                    최소공배수 = j;
                }
            }
            if(최대공약수==0) {
                최소공배수 = 최소공배수*i;
            }
            else {
            최소공배수 = 최소공배수*i/최대공약수;
            }
        }
        System.out.println(최소공배수);
    }
}

2019/11/28 20:50

big Ko


Run = True
value = 21

while Run:        
    count = 0
    for i in range(1,21):
        if value % i ==0:
            count += 1
    if count == 20:
        print(value)
        Run = False
    else :
        value +=1

초보자의 coding이라 시간이 어마어마 하게 걸립니다..ㅠㅜ

2020/01/28 00:33

semipooh

num=0
while(1):
num=num+(2357111317*19)
i=0
while (i<21):
i=i+1
if num%i!=0:
break
if (i>20):
print (num)
break

2020/03/15 07:50

Buckshot

from math import gcd
def sol(arr):
    def lcm(x, y):
        return x*y // gcd(x, y)
    while True:
        arr.append(lcm(arr.pop(), arr.pop()))
        if len(arr) == 1:
            return arr[0]

if __name__ == '__main__':
    print(sol([i for i in range(1, int(input('값을 입력하시오: '))+1)]))

2020/05/06 18:54

Hwaseong Nam

from math import gcd

def lcm(a,b):
    return int(a*b/gcd(a,b))

def small(n):
    n = list(range(1,n+1))
    while len(n) >= 1:
        n[0:2] = [lcm(n[0],n[1])]
        print(n[0:2])
    return n[0]
small(10)

math까진 생각해뒀는데 윗 풀이들을 통해 함수를 두개 만들어 풀이하는 것을 배웠습니다~

2020/05/15 11:24

Money_Coding

import math as m

def count_numer(number):

  for_answer=[2]

  for i in range(3,number+1,1):

    check=True

    for j in for_answer:

      if i%j==0:

        check=False

    if check==True:

      for_answer.append(i)

  return for_answer


def smallest_multiple(number):

  for_answer=[]

  for_answer_list=count_numer(number)

  for_dic={}

  for i in for_answer_list:

    for_dic[i]=0

  for k in range(2,number+1,1):

    for j in for_dic:

      count=0

      check=k

      while check%j==0:

        count+=1

        check=check/j

      if count>for_dic[j]:

        for_dic[j]=count

  answer=1

  for number in for_dic:

    answer=answer*m.pow(number,for_dic[number])

  return int(answer)

print(smallest_multiple(10))

print(smallest_multiple(20))

2021/01/08 16:25

전준혁

왕초보입니다. sieve(n)은 부스트코스-처음 배우는 프로그래밍 w파이썬의 자료를 참고했습니다.

def sieve(n):
    candidates = list(range(2, n))
    i = 0
    while i < len(candidates):
        prime = candidates[i]
        j = i + 1
        while j < len(candidates):
            if candidates[j] % prime ==0:
                candidates.pop(j)
            else:
                j += 1
        i += 1
    return candidates

def smallest_multiple(n):
    candidates = sieve(n+1)
    multiple = 1
    for i in range(len(candidates)):
        prime = candidates[i]
        power = 1
        while prime ** (power+1) < n:
            power += 1
        else:
            multiple = multiple * ((candidates[i]) ** power)
    return multiple

2021/01/22 23:26

팝코니

    let num=1;
    let cnt=0;

    while(true){
        for (let i = 1; i <= 20; i++) {
            if(num%i==0){
                cnt++
            }else{
                break;
            }
        }
        if(cnt==20){
            console.log(num); 
            break;
        }else{
            cnt=0;
            num++;
        }
    }

2021/02/16 13:53

wldus

number=0

while True:
    number+=1
    if number % 1 == 0 and number % 2 == 0 and number % 3 == 0 and number % 4 == 0 and number % 5 == 0 and number % 6 == 0 and number % 7 == 0 and number % 8 == 0 and number % 9 == 0 and number % 10 == 0 and number % 11 == 0 and number % 12 == 0 and number % 13 == 0 and number % 14 == 0 and number % 15 == 0 and  number % 16 == 0 and number % 17 == 0 and number % 18 == 0 and  number % 19 == 0 and number % 20 == 0 :
        print(number)
        break

2021/03/02 09:27

최우진

a=[]
for i in range(1,21):
    count=0
    for j in range(1,i+1):
        if i%j==0:
            count+=1
    if count==2:
        a.append(i)
a.remove(2)
a.remove(3)
count=0
while True:
    count+=1
    if 2**count>=10:
        a.append(2**count)
        break
count=0
while True:
    count+=1
    if 3**count>=7:
        a.append(3**count)
        break
total=1
for i in a:
    total*=i
print(a)
print(total)

2021/03/18 22:47

fox.j

def func1(num):
    n = int(num)
    number = 1
    div_result = []
    flag = True
    while flag:
        for i in range(1,n+1):
            if number % i == 0:
                div_result.append(0)
            else:
                div_result.append(1)

        if len(set(div_result)) == 1 and list(set(div_result))[0] == 0:
            print('FOUND!!!!', number)
            break
        else:
            number += 1
            div_result = []

func1(20)

2021/04/12 15:33

DSHIN

python 3.9.6입니다. 1~10 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수와 20!(팩토리얼) 사이에 값이 있다는 것을 이용했습니다. 또한, 이 범위 내에서 수는 11~20 사이의 모든 소수의 곱씩 건너뛰어도 됩니다. 소스 코드입니다.

import math
for num in range(2520*11*13*17*19, math.factorial(20), 11*13*17*19):
    status = True
    for test in range(11, 21):
        if num % test: status = False; break
    if status: print(num); break

실행 결과는 232792560입니다.

2021/07/20 09:44

이준우

#codingdojing_smallest_multiple

# 1. 중간까지 곱한다음에 젤 큰 숫자로 계속 올린다. 
# 2. 최소 공배수를 구한다. LCM = a*b/GCD

from functools import reduce

def GCD(a, b): #a>b , a<b여도 알아서 바뀜

    if a%b == 0: return b
    else: return GCD(b, a%b)

print(int(reduce(lambda x,y: x*y//GCD(x,y), range(1,21))))


2021/08/24 16:02

Jaeman Lee

ans = 1
num = 20

def a(n):
    a = True
    for i in range(2, n):
        if n % i == 0:
            a = False
            break
    return a


for i in range(2, num + 1):
    if a(i):
        ans *= i
        k = 2
        while True:
            if i**k <= num:
                ans *= i
                k += 1
            else:break


print(ans)

범위 사이에 존재하는 모든 소수의 가장 큰 자승수를 곱하면 나오네용

2021/09/27 17:42

Kim Hangil

def smallest_multiple(a):
    i = a
    while True:
        x = 0
        i += 2
        for y in range(1,a+1):
            if i%y == 0:
                x += 1
            else:
                break
    if x == a:
        return i

if __name__ == '__main__':
    a = 20
    print(smallest_multiple(a))

2021/10/27 19:27

서현준

n =1
result =1
while n!=20:
    if result%n ==0 :
       n+=1
    elif result%n !=0:
        result +=1
        n=1

print(result)

코드는 간단해서 맘에들지만.. 속도가 맘에 안드네요 효율화 하고싶어요

2022/01/07 23:07

양캠부부

n=20

import math

def math_lcm(a,b):
  return (a * b) // math.gcd(a,b)

def mlcm(n):
    a=[x for x in range(1,n+1)]
    lcm = 1

    for i in range(n):
        lcm = math_lcm(lcm, a[i])
    return lcm

mlcm(n) 

2022/02/16 19:38

로만가

package org.javaturotials.ex;
import java.util.*;
public class test {

    public static void main(String[] args) {
        int i=1;
        int num=0;
        int count=0;
        while(true) {
            for(int j=1; j<=20; j++) {
                if(i%j==0) {
                count++;
                }
            }
            if(count==20) {
                num=i;
                break;
            }
            count=0;
            i++;
        }
        System.out.println(num);
        }
        }

2022/02/20 12:36

Kkubuck

def get_multiple(num1, num2):
    for i in range(max(num1, num2), num1*num2+1):
        if i % num1 == 0 and i % num2 == 0:
            return i

max_len = 20
result = 2
for i in range(2, max_len+1):
    result = get_multiple(result, i)

print(result)

2023/04/04 15:07

HoHyeon Kim

n = 20
nums = [ x for x in range(1, n + 1)]
lcmNum = 1
for i in range(1, n):
    lcmNum *= nums[i]
    for j in range(i+1,n):
        if nums[j] % nums[i] == 0:
            nums[j] //= nums[i]
print(lcmNum)

2023/10/19 12:28

insperChoi

import math

nums = [x for x in range(1, 21)]
result = nums[0]

for num in nums:
    result = math.lcm(result, num)

print(result)

2026/05/15 00:31

우영재

목록으로