查看完整版本 : help!!! JAVA How to get prime number in random number

kelvin_0212 2013-7-18 03:56

range 100-20000.

public static int randomPrime(){
        boolean prime = true;         int i=2;    int p = (int) (100 + (Math.random() *20000));
    while (i<=Math.sqrt(p)){   
            if (p%i==0){                    p = (int) (100 + (Math.random() *20000));                    prime=false;                    }             i++;            }    if (prime) {
      System.out.println("p : "+ p);
    }   
}

fitcat07 2013-7-18 11:12

Please use code tag to show your code.

Susan﹏汪汪 2013-7-18 13:18

Prime checking裡面為何要再抽新數字...???

假設第一次抽出數字81 (((當係啦
當i == 3時p%i == 0即又再抽出新數字...假設是36
這時i依然在下個數字即4開始做test

這裡不是每次抽新數字時都重新做test的

另外...如果prime set左做false...就一直是false沒機會行output果行code



[url=http://www.discuss.com.hk/iphone][img=100,23]http://i.discuss.com.hk/d/images/r10/iphoneD.jpg[/img][/url]

fitcat07 2013-7-18 14:36

佢段碼重有其他問題,就咁講晒佢聽,相信佢唔會明。
我諗逐樣講會好尐。
但第一點要佢明白,貼程式碼上嚟,係要用代碼標籤。:loveliness:

P.S. 轉戶口?

Susan﹏汪汪 2013-7-18 14:44

是啊....另一邊壇個當了版主....心思思想換...:loveliness:

樓主段code不就是要重寫嗎...???XD

fitcat07 2013-7-18 14:55

係邊個版呀?

樓主係要重寫,但係學習應包括埋點樣問嘢,係咪?:loveliness:

Susan﹏汪汪 2013-7-18 14:58

[quote]原帖由 [i]fitcat07[/i] 於 2013-7-18 02:55 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367392923&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
係邊個版呀?

樓主係要重寫,但係學習應包括埋點樣問嘢,係咪?:loveliness: [/quote]同人創作~

琴晚諗到一點點玩意.... (((要挑戰Zuckerberg..???
好耐冇寫過網頁...不過試下可唔可以一日內寫好

[[i] 本帖最後由 Susan﹏汪汪 於 2013-7-18 03:01 PM 編輯 [/i]]

p_net 2013-7-18 18:04

我諗左一輪都唔知點解
原來 prime num = 質數..

my english is so poor. :smile_39:

but the above codes should retain a true/false result of the last MAX random num for increasing the efficiency. (staring looping from last MAX random num)

[[i] 本帖最後由 p_net 於 2013-7-18 06:56 PM 編輯 [/i]]

p_net 2013-7-18 18:54

[quote]原帖由 [i]Susan﹏汪汪[/i] 於 2013-7-18 02:58 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367393218&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
同人創作~

琴晚諗到一點點玩意.... (((要挑戰Zuckerberg..???
好耐冇寫過網頁...不過試下可唔可以一日內寫好 [/quote]a webpage can be finished in one single night??? :smile_40::funk:

i cant beleive even thought you are using any existing component or framework.. :smile_13:

Susan﹏汪汪 2013-7-18 19:01

[quote]原帖由 [i]p_net[/i] 於 2013-7-18 06:54 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367407256&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
a webpage can be finished in one single night??? :smile_40::funk:

i cant beleive even thought you are using any existing component or framework.. :smile_13: [/quote]沒試過怎麼知道... (((當年他都是用了一晚來做...

p_net 2013-7-18 19:08

[quote]原帖由 [i]Susan﹏汪汪[/i] 於 2013-7-18 07:01 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367407663&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
沒試過怎麼知道... (((當年他都是用了一晚來做... [/quote]you also say "at that time", i once surf its page at that time, the outlook is simple, not ajax, so it might be possible to finish with that quality (although i think that he was also blowing water for just using a night to finish). With current scale, i think you can even not "copy" it within a year.

Susan﹏汪汪 2013-7-18 19:37

[quote]原帖由 [i]p_net[/i] 於 2013-7-18 07:08 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367408035&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
you also say "at that time", i once surf its page at that time, the outlook is simple, not ajax, so it might be possible to finish with that quality (although i think that he was also blowing water fo ... [/quote]所以才說是用一日來做架構:loveliness:

p_net 2013-7-18 19:43

[quote]原帖由 [i]Susan﹏汪汪[/i] 於 2013-7-18 07:37 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367409533&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
所以才說是用一日來做架構:loveliness: [/quote]for just db efficiency, it has no way to beat it down... :smile_39:

try hard... :smile_13:

form5 2013-7-18 23:14

Just practicing Linq[code]using System;
using System.Linq;

class Program {
    static void Main() {
        var query = Enumerable.Range(100, 20000 - 100 + 1)
                        .Where(i => i % 2 != 0)
                        .Where(i =>
                            Enumerable.Range(2, (int)Math.Sqrt(i) - 1)
                                        .All(j => i % j != 0));


        int total = query.Count();
        Func<int, int> FindPrime = x => query.Skip(x).First();

        int random = new Random().Next(total);
        Console.WriteLine("{0}", FindPrime(random));
    }
}[/code]

Susan﹏汪汪 2013-7-19 03:24

其實如果唔係由2開始抽的話...可以淨抽單數的



[url=http://www.discuss.com.hk/iphone][img=100,23]http://i.discuss.com.hk/d/images/r10/iphoneD.jpg[/img][/url]

p_net 2013-7-19 11:31

[quote]原帖由 [i]Susan﹏汪汪[/i] 於 2013-7-19 03:24 AM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367436277&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
其實如果唔係由2開始抽的話...可以淨抽單數的



[img]http://i.discuss.com.hk/d/images/r10/iphoneD.jpg[/img] [/quote]but if the random num could be any value including even nums like 8, 12, 16, 18 ... etc. If just take odd nums, it will skip to check and give a incorrect answer.

fitcat07 2013-7-19 11:33

Just practicing Perl:
[code]#!/usr/bin/perl

# This subroutine will enter infinite loop when there
# no primes in the given range.
sub randomPrime {
        my $lo = $_[0];
        my $hi = $_[1];
        my $n;
        my $isPrime = 0;
        while (not $isPrime) {
                $n = $lo + int(rand($hi - $lo + 1));
                my $sqrtn = sqrt($n);
                $isPrime = 1;
                foreach (2..$sqrtn) {
                        if ($n % $_ == 0) {
                                $isPrime = 0;
                                last;
                        }
                }
        }
        $n;
}

print randomPrime(100, 20000), "\n";[/code]

遊鯤 2013-7-19 11:46

Prime number sequence: 2, 3, 5, 7, 11....
其中除了2和3,所有prime number都是6k+1或6k-1。
利用這一點,routine to check for prime number can be written as:[code]inline bool isPrime(unsigned n)
{
    if (n < 2)
        return false;
    if (n == 2 || n == 3)
        return true;
    if ((n & 1) == 0)
        return false;
    if (n % 3 == 0)
        return false;

    for (unsigned k = 1; 36*k*k-12*k < n; k++)
        if ((n % (6*k-1) == 0) || (n % (6*k+1) == 0))
            return false;

    return true;
}[/code]至於random prime number亦可以利用言個特性:[code]unsigned randomPrime()
{
    unsigned    nSeq = rand();

    if (nSeq == 0)
        return 2;
    if (nSeq-- == 1)
        return 3;

    do {
        unsigned    numToTest = 6 * nSeq -1;

        if (isPrime(numToTest))
            return numToTest;
        numToTest += 2;
        if (isPrime(numToTest))
            return numToTest;

        nSeq++;
    } while (true);
}[/code]上面的randomPrime還是略為粗糙,不夠random。

p_net 2013-7-19 12:29

6K 中的 K 是什麼? :smile_41:

Susan﹏汪汪 2013-7-19 12:57

[quote]原帖由 [i]p_net[/i] 於 2013-7-19 11:31 AM 發表 [url=http://www.discuss.com.hk/redirect.php?goto=findpost&pid=367449601&ptid=22195856][img]http://www.discuss.com.hk/images/common/back.gif[/img][/url]
but if the random num could be any value including even nums like 8, 12, 16, 18 ... etc. If just take odd nums, it will skip to check and give a incorrect answer. [/quote]
質數中只有2是雙數:smile_o05:



[url=http://www.discuss.com.hk/iphone][img=100,23]http://i.discuss.com.hk/d/images/r10/iphoneD.jpg[/img][/url]

Susan﹏汪汪 2013-7-19 12:58

[quote]原帖由 [i]遊鯤[/i] 於 2013-7-19 11:46 AM 發表 [url=http://www.discuss.com.hk/redirect.php?goto=findpost&pid=367450603&ptid=22195856][img]http://www.discuss.com.hk/images/common/back.gif[/img][/url]
Prime number sequence: 2, 3, 5, 7, 11....
其中除了2和3,所有prime number都是6k+1或6k-1。
利用這一點,routine to check for prime number can be written as:inline bool isPrime(unsigned n)
{
    if (n ... [/quote]強...又可以刪減了不必要的



[url=http://www.discuss.com.hk/iphone][img=100,23]http://i.discuss.com.hk/d/images/r10/iphoneD.jpg[/img][/url]

Susan﹏汪汪 2013-7-19 13:46

綀習~~[code]#include <iostream>
#include <functional>
#include <random>
using namespace std;


    const int prime[] = {5, 7, 11, 13, 17,
    19, 23, 29, 31, 37,
    41, 43, 47, 53, 59,
    61, 67, 71, 73, 79,
    83, 89, 97, 101, 103,
    107, 109, 113, 127, 131,
    137, 139, 149}; //total 33

int main()
{
    auto GetRandom = bind(uniform_int_distribution<int>(17, 3333), mt19937());
    //because 17 * 6 - 1 = 101 and 3333 * 6 + 1 = 19999
    auto add = bind(bernoulli_distribution(0.5), mt19937());
   
    bool getPrime = false;
    int result = 0;
    while(!getPrime)
    {
        getPrime = true;
        if(add())
            result = GetRandom() * 6 + 1;
        else
            result = GetRandom() * 6 - 1;
        for(int i = 0; i < 33 && result > prime[i]; ++i)
        {
            if(result % prime[i] == 0)
            {
                getPrime = false;
                break;
            }
        }
    }
   
    cout << result;
    return 0;
}[/code]

[[i] 本帖最後由 Susan﹏汪汪 於 2013-7-19 01:50 PM 編輯 [/i]]

遊鯤 2013-7-19 22:33

:smile_o12:
How about put it further to make a C++ class?[code]#include        <assert.h>
#include        <random>
#include        <iostream>

class prime_numbers_t
{
        std::vector<unsigned>        array;
        unsigned        from, to;
public:
        prime_numbers_t(unsigned from, unsigned to);
        unsigned        rangefrom() const {return from;}
        unsigned        rangeto() const {return to;}
        unsigned        size() const {return array.size();}
        unsigned        rand() const;
        const std::vector<unsigned>        numbers() const {return array;}
        inline bool isPrime(unsigned n)
        {
        //        if (n < 2)
        //                return false;
        //        if (n == 2 || n == 3)
        //                return true;
                if ((n & 1) == 0)
                        return false;
                if (n % 3 == 0)
                        return false;

                for (unsigned k = 1; 36*k*k-12*k < n; k++)
                        if ((n % (6*k-1) == 0) || (n % (6*k+1) == 0))
                                return false;

                return true;
        }
};

prime_numbers_t::prime_numbers_t(unsigned _from, unsigned _to)
:from(_from), to(_to)
{
        assert(from < to);

        if (from <=2)
                array.push_back(2);
        if (from <=3)
                array.push_back(3);

        unsigned        k = from/6 + 1;
        
        unsigned        n = 6 * k - 1;

        if (n < from && (n+2) < from) {
                k++;
                n = 6 * k - 1;
        }

        while (n < to) {
                if (from <= n && n <= to && isPrime(n))
                        array.push_back(n);

                n += 2;

                if (n <= to && isPrime(n))
                        array.push_back(n);

                k++;

                n = 6 * k -1;
        }
}

unsigned prime_numbers_t::rand() const
{
        std::random_device        rd;
        std::mt19937        gen(rd());
        std::uniform_int_distribution<unsigned>        dis(0, size()-1);

        auto        sourceVec = numbers();
        unsigned        idx = dis(gen);

        return sourceVec[idx];
};

int main()
{
        prime_numbers_t        primeNumbers(0, 4000);

        std::cout << "rime numbers in the range [" << primeNumbers.rangefrom()
                  << "..." << primeNumbers.rangeto() << "]:"
                        << std::endl;

        for (size_t n = 0; n < primeNumbers.size(); n++)
                std::cout << primeNumbers.numbers()[n] << ' ';

        std::cout << std::endl
                  << "10 random numbers in the range [" << primeNumbers.rangefrom()
                  << "..." << primeNumbers.rangeto() << "]:"
                        << std::endl;

        for (size_t n = 0; n < 10; n++)
                std::cout << primeNumbers.rand() << ' ';

        std::cout << std::endl;

        return 0;
}[/code]

[[i] 本帖最後由 遊鯤 於 2013-7-25 11:32 AM 編輯 [/i]]

Susan﹏汪汪 2013-7-19 23:06

[quote]原帖由 [i]遊鯤[/i] 於 2013-7-19 10:33 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367488510&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
:smile_o12:
How about put it further to make a C++ class?#include  
#include  
#include  

class prime_numbers_t
{
     std::vector    array;
     unsigned  from, to;
public:
     prime_nu ... [/quote]static random number engine才會發揮到作用的...:smile_13:
我果段code都只是參考下...

form5 2013-7-21 00:38

any one line program? :smile_38: :smile_38:

me888 2013-7-23 12:15

自已將 pascal 轉 Java 或者 C :loveliness:
[color=#111111][/color]
[color=#111111][/color]
[color=#111111]program PRIMENUM(input, output);[/color]
[color=#111111]label 1;[/color]
[color=#111111]label 2;[/color]
[color=#111111]var[/color]
[color=#111111]    z : longword;[/color]
[color=#111111]    x : longword;[/color]
[color=#111111]    v : longword;[/color]
[color=#111111]begin[/color]
[color=#111111]writeln('                                          Created by ME888 ');[/color]
[color=#111111]writeln('=======================================================================');[/color]
[color=#111111]writeln('Press enter to start computing the prime numbers;  Press Pause to halt.');[/color]
[color=#111111]write('=======================================================================');[/color]
[color=#111111]readln;[/color]
[color=#111111]write('2 3 ');[/color]
[color=#111111]z := 1;[/color]
[color=#111111]goto 2;[/color]
[color=#111111]1:[/color]
[color=#111111]if z mod x = 0 then begin[/color]
[color=#111111]    goto 2;[/color]
[color=#111111]    end;[/color]
[color=#111111]if x <= ( z div (v))-2 then begin[/color]
[color=#111111]    v:=x;[/color]
[color=#111111]    x:=x+2;[/color]
[color=#111111]    goto 1;[/color]
[color=#111111]  end;[/color]
[color=#111111]write(z);[/color]
[color=#111111]write(' ');[/color]
[color=#111111]2:[/color]
[color=#111111]z := z + 2;[/color]
[color=#111111]x := 3;[/color]
[color=#111111]v := 2;[/color]
[color=#111111]goto 1;[/color]
[color=#111111]end.[/color]

fitcat07 2013-7-23 12:26

goto?:funk:

Susan﹏汪汪 2013-7-23 14:13

還有人用goto的...??:smile_41:



[url=http://www.discuss.com.hk/iphone][img=100,23]http://i.discuss.com.hk/d/images/r10/iphoneD.jpg[/img][/url]

me888 2013-7-23 21:24

有 label 1:同 2:



[url=http://www.discuss.com.hk/android][img=100,23]http://i.discuss.com.hk/d/images/r10/androidD.jpg[/img][/url]

form5 2013-7-23 21:58

[quote]原帖由 [i]me888[/i] 於 2013-7-23 09:24 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=367758912&ptid=22195856][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
有 label 1:同 2:



[img]http://i.discuss.com.hk/d/images/r10/androidD.jpg[/img] [/quote]

:smile_o04:  I'm loving it, but I'm feeling vomiting.

[[i] 本帖最後由 form5 於 2013-7-23 10:01 PM 編輯 [/i]]
頁: [1] 2
查看完整版本: help!!! JAVA How to get prime number in random number