Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2004
    Posts
    30

    Unanswered: Some math logics

    I have 2 questions.

    1) Lets say we have a table

    CREATE TABLE TEST
    (
    N INT
    )

    These table have 10 records - the numbers from 1 to 10.

    I need 1 query ONLY which will update the table and make it with 100 records - the numbers from 1 to 100.

    2) How with 1 query ONLY i have select only the prime numbers

  2. #2
    Join Date
    Jan 2004
    Posts
    49
    if exists(select 1 from tempdb..sysobjects where id = object_id('tempdb..#test'))
    drop table #test
    go
    create table #test(n int)
    go
    insert into #test(n)
    select 1 union all select 2 union all select 3
    union all select 4 union all select 5 union all select 6
    union all select 7 union all select 8 union all select 9 union all select 10
    go
    select * from #test
    go
    insert into #test(n)
    select t2.n + 10*(t1.n-1)
    from #test t1 cross join #test t2
    where t2.n + 10*(t1.n-1) > 10
    go
    select * from #test order by 1
    go

    what is "prime numbers" ?????

  3. #3
    Join Date
    Jan 2004
    Posts
    30

    Exclamation

    Prime numbers

    1, 2, 3, 5, 7, 11, 13, 17, 19, etc...

    You can divide them by 1 and by the number itself.

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    insert into YourTable (N)
    select YourTable1.N * YourTable2.N
    from YourTable YourTable1,
    YourTable YourTable2
    where not exists (select * from YourTable where N = YourTable1.N * YourTable2.N)

    ...will populate your table. As far as the Prime Numbers go, the greatest mathematical minds in history have been unable to come up with a non-exhaustive method of determining whether a number is Prime, so don't feel bad if you are unable to solve this.

    You may be able to use a method know as Aritosthenes' Sieve to populate a table with prime numbers, but rolling it into a single SQL statement will be a challenge. If I have time, I'll give it a shot.

    By the way, why the insistence on a single statement?
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  5. #5
    Join Date
    Mar 2003
    Location
    Australia
    Posts
    59
    The-Saint,

    Prime Numbers in TSQL
    2) http://weblogs.sqlteam.com/davidm/ar...10/30/412.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •