본문 바로가기

Programming

(136)
홀짝 판별 1. n % 2 == 0 짝수의 수학적 정의는 '2로 나누어 떨어지는 정수'다. 바꿔 말하면 '2로 나눴을 때 나머지가 0인 정수'이며, 이를 식으로 표현하면 위와 같다. 정의에 입각한 식이기 때문에 통상적으로 쓰이는 방법이다. 2. n&1 == 0 1의 이진수는 00000001이다. 즉 LSB(Least Significant Bit, 제일 오른쪽 비트)가 1이다. 반면 짝수를 이진수로 나타내면 bbbbbbb0이 된다(b는 임의의 비트). 즉 LSB는 무조건 0이다. 즉 1과 짝수의 LSB는 서로 다를 수밖에 없다. 따라서 (짝수) AND 1은 0이 되고, (홀수) AND 1은 1이 된다. $Let\; n\,=\, 13$ $then$ $n\; AND\; 1$ $=\;00001011_{(2)}$ $\;\;..
[Project Euler] Problem 005 이미 푼 문제다.(https://t0pli.tistory.com/49) 근데 풀고 1년 정도 지나서 다시 봤더니 뭔 말인지 하나도 모르겠어서 그냥 다시 풀었다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.math.BigInteger; import java.util.List; import java.util.ArrayList; public class Q005 { public static void main(String[] args) { System.out.println(run()); } public static String run() { List list = new ArrayList(0); for(long i = 1L;..
[Project Euler] Coding Quiz 4 1억 번째 컬럼명만 구하라길래 입력은 따로 안 받았음. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Q004 { public static void main(String args[]) { System.out.println(run()); } public static String run() { int n = 100000000; StringBuilder sb = new StringBuilder(); sb.append((char)('A' + (((26 + ((n%26) - 1)))%26))); while((n /= 26) > 26) { sb.append((char)('A' + (((26 + ((n%26) - 1)))%26))); } sb.append((char)('..
[Project Euler] Coding Quiz 1 출처가 사이냅소프트 채용퀴즈라길래 뭐하는 데인가 했더니 이 사이트를 운영하는 회사였다. 사이트 첫 페이지(https://euler.synap.co.kr/)에서 확인할 수 있다. 원래는 SW 회사인 것 같고 이 문제를 당사 채용 문제로 출제한 모양이다. 아무튼 나는 이 문제의 더 어려운 버전인 92번 문제를 푼 뒤에 이 문제를 풀었기 때문에 쉽게 풀 거라고 생각했다. 하지만 어림도 없지 ㅋㅋ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class Main { public static void main(String args[]) { System.out.println(run()..
[Project Euler] Problem 092 문제가 잘 안 보인다..어떤 자연수에 대해 각 자릿수의 제곱의 합을 구하는 과정을 반복하면 어떤 수든 1이나 89로만 끝나게 되는데, 1천만 미만의 자연수 중에 89로 끝나는 수는 몇 개인지 구하는 문제다. 원래 풀려던 건 이거였다. 92번이 더 어렵다길래 한 번 봤더니 풀 수 있을 것 같아서 그냥 풀었다. 123456789101112131415161718192021222324252627282930public class P092 { public static void main(String args[]) { System.out.println(run()); } public static String run() { int n = 9999999; int count = 0; while(n > 0) { int temp..
[Project Euler] Problem 036 1234567891011121314151617181920212223242526272829public class P036 { public static void main(String args[]) { System.out.println(run()); } public static String run() { int n = 1000000; int count = 0; do { if(isSymmetric(Integer.toString(n)) && isSymmetric(Integer.toBinaryString(n))) { count += n; } } while(n-- > 0); return Integer.toString(count); } public static boolean isSymmetric(String s) { ..
[Project Euler] Problem 034 1234567891011121314151617181920212223242526272829303132public class P034 { public static void main(String args[]) { System.out.println(run()); } public static String run() { int count = 0; for(int i = 10; i 0); if(sum == i) { count += i; } } return Integer.toString(count); } public static int factorial(int n) { int res = 1; for(int i = 2; i sum = sum + 3!;temp = 1863/10; -> temp = 186; sum = sum..
[Project Euler] Problem 037 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768public class Problem037 { public static void main(String[] args) { System.out.println(run()); } public static String run() { int n = 11; String temp; boolean prime; int sum = 0; int len; for(int count = 1; count