mathematics and me

We must know, We will know。 -----Hilbert

2014年5月14日星期三

Project Euler答题(不定时更新)

继续答题。

2014.5.14

第11题(python):

grid = """08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
          49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
          81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
          52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
          22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
          24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
          32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
          67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
          24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
          21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
          78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
          16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
          86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
          19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
          04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
          88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
          04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
          20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
          20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
          01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""

grid = grid.replace('\n', '').replace(' ', '')
# creat a matrix
matrix = [[0 for i in range(20)] for i in range(20)]
for i in range(20):
    for j in range(20):
        matrix[i][j] = int(grid[2*(20*i + j):2*(20*i + j) + 2])
        #print matrix[i][j]

# find the largest of rows
lor = 0 # the largest of rows
for i in range(20):
    for j in range(17):
        if matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3] > lor:
            lor = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]

# find the largest of cols
loc = 0 # the largest of cols
for i in range(17):
    for j in range(20):
        if matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j] > loc:
            loc = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]

# find the largest of diagonal lines
lod = 0 # the largest of diagonal lines
for i in range(17):
    for j in range(17):
        if matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3] > lod:
            lod = matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3]
# find the largest of anti diagonal lines
load = 0 # the largest of anti diagonal lines
for i in range(17):
    for j in range(3, 20):
        if matrix[i][j] * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3] > load:
            load = matrix[i][j] * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3]

print max(lor, loc, lod, load)

2014年5月8日星期四

Project Euler答题(6--10)

为防止帖子太长,故新开一贴,继续答题。
2014.5.8

第六题(python):

sum_of_squares = 0
sum_of_number = 0

for i in range(1, 101):
    sum_of_squares = sum_of_squares + i*i

for i in range(1, 101):
    sum_of_number = sum_of_number + i

print sum_of_number**2 - sum_of_squares

2014.5.8

第七题(python):

import math
import random
import sys   
sys.setrecursionlimit(1000000)

# def is_prime(num):
#     i = 2
#     isprime = True
#     while i <= int(math.sqrt(num)):
#         if num % i == 0:
#             isprime = False
#             break
#         i = i + 1
#     return isprime

# def is_prime(num):
#     if num % 2 == 0:
#         return False;
#     i = 3
#     isprime = True
#     while i <= int(math.sqrt(num)):
#         if num % i == 0:
#             isprime =  False
#             break;
#         i = i + 2
#     return isprime

# prime_list = [2, 3]
# def is_prime(num):
#     isprime = True
#     i = 0
#     while i < len(prime_list):
#         if num % prime_list[i] == 0:
#             isprime = False
#             break
#         i = i + 1
#     return isprime

prime_list = [2, 3]
def is_prime(num):
    isprime = True
    i = 0
    while i < len(prime_list) and prime_list[i] <= int(math.sqrt(num)):
        if num % prime_list[i] == 0:
            isprime = False
            break
        i = i + 1
    return isprime

# def is_prime(num):
#     f = 5
#     isprime = True
#     r = int(math.sqrt(num)) + 1
#     if num % 2 == 0 or num % 3 == 0:
#         return False
#     while f <= r:
#         if num % f == 0:
#             isprime = False
#         elif num % (f + 2) == 0:
#             isprime = False
#         f = f + 6
#     return isprime

# Wilson' theroem
# def factorial(n):
#     if (n == 1):
#         return n
#     else:
#         return n * factorial(n - 1)
# def is_prime(num):
#     if (factorial(num - 1) + 1) % num == 0:
#         return True
#     else:
#         return False

# Fermat's little theroem
# def FermatPrimalityTest(number):
#     for time in range(10):
#         randomNumber = random.randint(2, number - 1)
#         if ( pow(randomNumber, number-1, number) != 1 ):
#             return False
#     return True


number_of_prime = 2
num = 5
while True:
    if is_prime(num):
        prime_list.append(num)
        number_of_prime = number_of_prime + 1
        if number_of_prime == 40001:
            print num
            break
    # if is_prime(num) == False and FermatPrimalityTest(num) == True:
    #     print num
    num = num + 2

2014年5月7日星期三

复旦大学android客户端开发计划

        最近android的知识也学得差不多了,遂决定做个小应用玩一玩。转头一看发现市场上貌似什么应用都有了,不需要我再去做什么。于是想到了要做一个集成我旦一些服务的应用,这些服务包括但不仅限于复旦邮箱(重点打造),复旦图书馆,e-learning,urp服务等。未来也许还会添加其他功能。
        为什么要做这个东西?其实一开始我只是想把邮箱功能做好。虽然市场上已经有不少邮箱的app了,但是这些应用要么界面粗糙,要么不支持复旦邮箱,最重要的是它们都没有为复旦邮箱量身订做的功能!!!为什么呢?因为复旦邮箱有复旦“特色”,经常充斥着各种“求填问卷”,“社团广告”等。因为我的手机邮箱里设置了提醒功能,以便随时能接受重要邮件。一有新邮件,就会自动提醒。每天被这些“垃圾邮件”骚扰五遍以上,你知道我的感受吗!!!扫邮什么的最烦了,必须把它屏蔽掉!!!最近我突然发现了这些“问卷贴”的一个共同特点(虽然很明显),利用这个特点,我就可以轻松地把这些邮件屏蔽掉!!!这个特(ruo)点(dian)是什么暂时先保留,以免“道高一尺魔高一丈”。
        另一个想做好的功能是图书馆的各项服务。从此只要手机在手,你就可以轻松查询,预约,续借图书。虽说浏览器也能做到,但毕竟不适合在手机端浏览。
        目前基本框架已搭好,剩下的就是把各项功能添加进去了。我是极为注重用户体验的,所以花了很多时间来做UI。没有一个好的界面,用起来怎么可能爽!虽然我不是一个专业的UI/UX designer,但我会尽我所能把UI做好。我们并不缺技术(technology),缺少的是人文(liberal arts)关怀。所以我一直都很佩服apple,佩服乔帮主,apple是少数能把技术和人文光怀都做到极致的公司。我也一直很喜欢apple的各项产品,虽然我目前没有经济能力去拥有它。
科技与人文的十字路口
        目前项目托管在Bitbucket,项目地址:https://bitbucket.org/egrcc/fudanclient ,由于项目尚在开发中,所以不会放到github上去,等项目完成后会迁移到github上来。由于最近事情比较多,再过一段时间又要到学期末了,估计开发会很缓慢,希望在暑假我能把这个项目做完。

2014年3月24日星期一

Project Euler答题(1--5)

发现一个很不错的Project Euler,用空的时候做一做。希望我能坚持。

2014.3.24

第一题(java):

public class ProjectEuler1 {
   public static void main(String[] args) {
        int a = 0;
        int b = 0;
        for(;a < 1000;a++){
            if(a%3 == 0 || a%5 == 0){
         b = b + a;
            }
        }
        System.out.println(b);
   }
}

2014.3.26

第二题(java):

import java.util.ArrayList;

class ProjectEuler2{
    public static void main(String[] args){
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(1);
        arrayList.add(2);
        int i = 0;
        while(arrayList.get(i) + arrayList.get(i + 1) < 4000000){
            arrayList.add(arrayList.get(i) + arrayList.get(i + 1));
            i++;
        }
        int b = 0;
        for(int a = 0;a <= (i + 1);a++){
            if(arrayList.get(a)%2 == 0){
                b = b + arrayList.get(a);
            }
        }
        System.out.println(b);
    }
}

2014年2月21日星期五

时间都去哪儿了

        不知不觉一个寒假又结束了。此刻的我又踏上了回复旦的路。
        时间都去哪儿了?每次回家都带着一大堆书,结果又看了多少?
        这次寒假共一个月左右,大体总结了下:
        前十天mathematica入了门。在我遇见它之前,我真没想到世间竟有如此神器(可以被称为神器的软件还真不多)。它的数学运算能力自然不用说,最让我没想到的是,它作为一个“ 编辑器 ”所排版出来的文章也如此漂亮。每次看到自带的帮助文档,都被它精美的排版所折服,个人感觉完全不逊于word(虽然在排版上不如word方便)。更重要的是它能方便地做许多Word很难做到的事情,特别是在写数学文章方面。比如数学公式的输入,数学结果的计算和展现。如果我在这之前没有学过\(\LaTeX\),我一定会拿mathematica来记数学笔记,写数学文章。即使有了 \(\LaTeX\),我认为它仍然是一个不错的选择。当然,我现在的水平仍在入门阶段,我接触到的或许还只是冰山一角,非常期待后续的学习。 这个学期课不多,我的学习重心一部分应该会放在这上面。 计算机方面,这十天还接触了下python,未深入。
        中间十天主要学习了下个学期要学的内容。下学期有复分析,实分析,拓扑,还有数学模型(我不打算选,应该会改为数论)。本来还想选分形几何的,考虑到我的mathematica水平还不高,打算下学期再选(因为这门课的考核方式之一是编制计算机程序设计分形图形)。说来惭愧,每次我都完成不了自己之前制定的计划,这次也不例外。说实话,我始终感觉自己在数学上没有什么前途,跟丘成桐比少了勤奋,跟陶哲轩比少了天赋。
        最后十天基本上没有学习。因为我表弟从广州回来了,我的电脑基本上就被他给抢占了,而且家里的人太多,实在不利于学习。表弟只会玩游戏,但很不巧,我的电脑是linux系统,基本上没什么游戏可玩,他在windows上玩过的游戏在我这都玩不了。最后装了个虚拟机,但虚拟机毕竟配置太低,只能玩一玩红警这类游戏。后来又在软件中心找到了几个即时战略游戏,比如warzone 2100(不知有没有写错),表弟也玩得不亦乐乎。此外一件事情是终于在linux上实现了wifi共享(见前一篇blog)。
        除了以上做的事情,我寒假大部分时间都花在了睡觉上,自己想想都觉得惭愧。希望以后有所改善。

2014年2月19日星期三

linux下wifi共享——virtualbox虚拟机+小度wifi

      很久没有更博了,最近事情比较多。
      之前试过很多方法想在linux下wifi共享都没有成功,没想到今天成功在linux下用虚拟机实现了wifi共享。
      linux下的wifi共享没有windows那么方便。话说我之前使用win7的时候,只需要打几行代码就能成功实现wifi共享,没想到linux下这么麻烦。我的操作系统是ubuntu13.10,之前在网上寻找方法时,有人提到ubuntu本身就有wifi共享的功能,只要在网络连接那里创建新的wifi网络,之后一步步按提示设置就可以,但是这样弄出来的wifi的模式却不被安卓手机支持。当然,网络上还有其他一些方法,但比较麻烦,我尝试过,但没有成功,还差点把我的网络整个给弄没了。所以只能作罢。之后我在virtualbox里装了win7,win8,xp系统,想用以前在windows里的方法试一试,结果打代码的方式没有成功,用各种wifi共享软件也没有成功。当时也由于手头上还有其他的事情,就没有再管这事了。
      寒假回到家中,由于家里没有路由器, 故又折腾起来了。万万没想到,这一次成功了。下面讲一讲我的方法。
      我的系统:ubuntu13.10,工具:virtualbox虚拟机,xp系统镜像,小度wifi。
      先在虚拟机里装好xp系统, 可以装精简版。然后要让虚拟机能够识别出usb设备,要去装一个扩展包(不是增强功能包)。在xp里装好小度的驱动,然后在主机上插上小度wifi,再让虚拟机接管此usb设备。等待一段时间就ok了。
       在xp+小度wifi实测通过,目测360随身wifi也可以。 在win7或win8上没有通过,原因是在虚拟机里ics服务不知为何无法启动,目前未解决。

2014年2月6日星期四

我第一次开blog写的话

我第一次开blog是在wordpress,因为当时看见几个数学家都在上面,而且只知道wordpress是支持\(\LaTeX\)。后来不知怎么,我那个blog竟然访问不了了,于是无奈转到了Blogger上。没想到后来也把\(\LaTeX\)给弄好了。今天突然发现,我的那个blog又能访问了,于是把当时第一次写blog时写的话转过来。半年前写的了,现在看来并不全对,所以并不适用于开这个博客的初衷。全文如下:

写在前面
      没有想到我也会有开博客的一天,按照惯例,在第一篇博客中说说为什么我会开博客,以及为什么选择wordpress。
  前几天不经意间看到刘未鹏的文章为什么你应该(从现在开始就)写博客, 感觉他说的很对。自己自从进入大学以来,接触到的新知识猛增,再也不像以前高中时那样重复的做题。明天都感觉自己接触到的知识太多,而又没有把这些记下 来,于是就萌生了开博客记录学到的知识及自己的想法的念头。在前一年的大学生活中,接触到了现代数学的宏伟大厦,深感现代数学的知识繁多,一心一意只想把 这些知识都去了解,于是看了很多书。但我的学习只是停留在看书上,而少于思考。很多东西自己看懂了,却没有真正思考过。在刘未鹏的文章中有这样几句话:书 写是为了更好的思考;“教”是最好的“学”。有些东西自己看懂了,但要把它写出来并不容易。要讲给别人听,并让别人听懂就更不易了。所以打算在此记录一下 自己学到的新东西,并尽量把它表达出来。
  再说说为什么选择wordpress。首先当然是因为wordpress支持\LaTeX, 这样才能方便的输入数学公式。其次是因为wordpress在墙外,一般人难以看到。我写博客并不是给别人看,而是为了记录自己大学四年(唉,实际只剩三 年了)学到的方方面面,因为我觉得大学的学习实在很值得去记录。所以这篇写在前面也是写给我自己看的,以免我以后忘了当初开博客的初衷。所以如果你如果你 费劲千辛万苦翻越高高的围墙,而又“不幸”来到了我的博客,并且又点开了这篇文章,那真的是我的莫大荣幸了。
  记得一年前就看到了Terence Tao大神在wordpress上开的博客,真没想到自己有一天也会在wordpress上开博客,这个世界真的很奇妙,未来总是不可预见的。

2014年2月3日星期一

(转)Mathematica学习资源

本文转自Mathematica Stack Exchange上问题“Where can I find examples of good Mathematica programming practice?”
这是第一个回答Faysal Aberkane):
Here's a collection of resources that I started on Mathgroup (A collection of Mathematica learning resources ) and updated here at StackOverflow. As this site is dedicated to Mathematica it makes more sense to maintain it here. This represents a huge amount of information; of course it's not exhaustive so feel free to improve it! Also, don't hesitate to share it and suggest other interesting links! Remember, you can always search the online Documentation Center of Mathematica, that is identical to the built-in help of the latest software version.
Links to more advanced aspects of the program that you can start to appreciate once you understand the basics are provided in separate answers (below) as this post became too large.

Introduction

Basic advices for people new to Mathematica

Functional style
Avoid iterative programming using loops like For or Do, use instead functional programming functions Map, Scan, MapThread, Fold, FoldList, ... and pure functions. This makes the code cleaner and faster.
Transpose and dimensions
  • Something not easy to guess alone at the beginning: if you have x={1,2} and y={3,4}, doing Transpose[{x,y}] or {x,y}ESC tr ESC in the front end will produce {{1,3},{2,4}} (format compatible with ListPlot). This animation helps understand why.
  • You can also use the second argument of Transpose to reorder the indices of a multidimensional list.
  • Don't forget to regularly control the output of the lists you generate using Dimensions.
Get familiar with shorthand syntax (@, &, ##, /@, /., etc.)
  • Operator Input Forms
  • It may happen that you are not sure about which shorthand expression will get executed first, in this case test the correctness of your input using something like Hold[inputExpression]//FullForm
Programming easily
  • Getting help: Execute ?Map for example for a short description of a function, or press F1 on a function name for more details and examples about it. You can solve many problems by adapting examples to your needs.
  • Auto-completion: Start typing the name of a function and (in Mathematica 9+) select from the pop-up auto-completion menu, or press Ctrl+k to get a list of functions which names start with what has already been entered. Once the name of the function is written completely press Ctrl+Shift+k (on Mac, Cmd+k) to get a list of its arguments.
  • Function templates: In Mathematica 9, after typing a function name, press Ctrl+Shift+k (on Mac, Cmd+Shift+k) and click on the desired form from the pop-up menu to insert a template with named placeholders for the arguments.
    Other useful shortcuts are described in the post Using the Mathematica front-end efficiently for editing notebooks.
  • Use palettes in the Palettes menu especially when you're beginning.
  • In Mathematica 8, use the natural input capability of Wolfram Alpha, for example type "= graph 2 x + 1 between 0 and 3" without the quotes and see the command associated with the result.

2014年2月1日星期六